Skip to content

Commit

Permalink
chore: add method to convert genders to advantage known genders
Browse files Browse the repository at this point in the history
Signed-off-by: Kathurima Kimathi <[email protected]>
  • Loading branch information
KathurimaKimathi committed Aug 2, 2024
1 parent 83bef68 commit 8198348
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 26 deletions.
54 changes: 29 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,55 @@ env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
golangci:
strategy:
matrix:
go-version: [1.21.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest

lint_and_test:
needs: [golangci]
strategy:
matrix:
go-version: [1.16.x]
go-version: [1.21.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
timeout-minutes: 80
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Install Go dependencies
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.37.1
go get -u github.com/kisielk/errcheck
go get -u golang.org/x/lint/golint
go get -u honnef.co/go/tools/cmd/staticcheck
go get -u github.com/axw/gocov/gocov
go get -u github.com/securego/gosec/cmd/gosec
go get -u github.com/ory/go-acc
go get -u github.com/client9/misspell/cmd/misspell
go get -u github.com/gordonklaus/ineffassign
go get github.com/fzipp/gocyclo
go get github.com/stretchr/testify/[email protected]
go mod download
go get github.com/axw/gocov/gocov
go get github.com/ory/go-acc
go install github.com/ory/go-acc
go install github.com/axw/gocov/gocov
go get github.com/savannahghi/[email protected]
- name: Run lint and test
- name: Run tests
run: |
staticcheck ./...
go fmt $(go list ./... | grep -v /vendor/)
go vet $(go list ./... | grep -v /vendor/)
golint -set_exit_status $(go list ./... | grep -v /vendor/)
errcheck -ignore 'os:.*,' $(go list ./... | grep -v /vendor/)
misspell -error .
gosec -exclude=G304,G101 ./...
go-acc -o coverage.txt --ignore generated,cmd ./... -- -timeout 60m
grep -v "generated.go" coverage.txt > coverage.out
go-acc -o coverage.txt --ignore generated,cmd,graph ./... -- -timeout 60m
grep -v "generated.go" coverage.txt | grep -v "_gen.go" | grep -v "_mock.go" | grep -v "*mock.go" | grep -v "mocks.go" | grep -v "*resolver*go" | grep -v "server.go" | grep -v "*.html" > coverage.out
go tool cover -html=coverage.out -o coverage.html
gocov convert coverage.out > coverage.json
gocov report coverage.json > coverage_report.txt
tail coverage_report.txt
- name: Install goveralls
env:
GO111MODULE: off
Expand Down
13 changes: 13 additions & 0 deletions enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ func (e Gender) String() string {
return string(e)
}

// ToAdvantageGender converts the `male` and `female` genders to `MALE` and `FEMALE` respectively.
// This is used in support of advantage wrapper service and also ensure consistency of known gender types in go services.
func (e Gender) ToAdvantageGender() string {
switch e {
case GenderMale:
return "MALE"
case GenderFemale:
return "FEMALE"
default:
return e.String()
}
}

// UnmarshalGQL translates from the supplied value to a valid enum value
func (e *Gender) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
Expand Down
31 changes: 31 additions & 0 deletions enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,3 +1185,34 @@ func TestSenderID_String(t *testing.T) {
})
}
}

func TestGender_ToAdvantageGender(t *testing.T) {
tests := []struct {
name string
e enumutils.Gender
want string
}{
{
name: "Happy case: convert `male` to `MALE`",
e: enumutils.GenderMale,
want: "MALE",
},
{
name: "Happy case: convert `female` to `FEMALE`",
e: enumutils.GenderFemale,
want: "FEMALE",
},
{
name: "Happy case: return other genders",
e: enumutils.GenderNonBinary,
want: string(enumutils.GenderNonBinary),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.ToAdvantageGender(); got != tt.want {
t.Errorf("Gender.ToAdvantageGender() = %v, want %v", got, tt.want)
}
})
}
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/savannahghi/enumutils

go 1.16
go 1.22.1

require github.com/stretchr/testify v1.7.0

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

0 comments on commit 8198348

Please sign in to comment.