Skip to content

Commit

Permalink
feat: add gender enums (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmuhia committed Jun 19, 2024
1 parent 273b983 commit e77d9a4
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
47 changes: 47 additions & 0 deletions enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type IdentifierType string

type ContactType string

type GenderType string

const (
// Identifier types
IdentifierTypeNationalID IdentifierType = "NATIONAL_ID"
Expand All @@ -30,6 +32,16 @@ const (
ContactTypeEmail ContactType = "EMAIL"
)

const (
GenderTypeMale GenderType = "MALE"
GenderTypeFemale GenderType = "FEMALE"
GenderTypeOther GenderType = "OTHER"
// GenderTypeASKU stands for Asked but Unknown
GenderTypeASKU GenderType = "ASKU"
// GenderTypeUNK stands for Unknown
GenderTypeUNK GenderType = "Unknown"
)

// IsValid returns true if a contact type is valid
func (f ContactType) IsValid() bool {
switch f {
Expand Down Expand Up @@ -110,3 +122,38 @@ func (f *IdentifierType) UnmarshalGQL(v interface{}) error {
func (f IdentifierType) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(f.String()))
}

// IsValid returns true if a Gender type is valid
func (f GenderType) IsValid() bool {
switch f {
case GenderTypeMale, GenderTypeFemale, GenderTypeOther, GenderTypeASKU, GenderTypeUNK:
return true
default:
return false
}
}

// String converts the Gender type enum to a string
func (f GenderType) String() string {
return string(f)
}

// UnmarshalGQL converts the supplied value to a Gender type.
func (f *GenderType) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return fmt.Errorf("enums must be strings")
}

*f = GenderType(str)
if !f.IsValid() {
return fmt.Errorf("%s is not a valid ContactType type", str)
}

return nil
}

// MarshalGQL writes the gender type to the supplied writer
func (f GenderType) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(f.String()))
}
123 changes: 123 additions & 0 deletions enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,126 @@ func TestContactType_MarshalGQL(t *testing.T) {
})
}
}

func TestGenderType_String(t *testing.T) {
tests := []struct {
name string
e GenderType
want string
}{
{
name: "happy case: enum to string",
e: GenderTypeMale,
want: "MALE",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.String(); got != tt.want {
t.Errorf("GenderType.String() = %v, want %v", got, tt.want)
}
})
}
}

func TestGenderType_IsValid(t *testing.T) {
tests := []struct {
name string
e GenderType
want bool
}{
{
name: "valid type",
e: GenderTypeMale,
want: true,
},
{
name: "invalid type",
e: GenderType("invalid"),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.e.IsValid(); got != tt.want {
t.Errorf("GenderType.IsValid() = %v, want %v", got, tt.want)
}
})
}
}

func TestGenderType_UnmarshalGQL(t *testing.T) {
value := GenderTypeFemale
invalid := GenderType("invalid")

type args struct {
v interface{}
}

tests := []struct {
name string
e *GenderType
args args
wantErr bool
}{
{
name: "valid type",
e: &value,
args: args{
v: "FEMALE",
},
wantErr: false,
},
{
name: "invalid type",
e: &invalid,
args: args{
v: "this is not a valid type",
},
wantErr: true,
},
{
name: "non string type",
e: &invalid,
args: args{
v: 1,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.e.UnmarshalGQL(tt.args.v); (err != nil) != tt.wantErr {
t.Errorf("GenderType.UnmarshalGQL() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestGenderType_MarshalGQL(t *testing.T) {
w := &bytes.Buffer{}

tests := []struct {
name string
e GenderType
b *bytes.Buffer
wantW string
panic bool
}{
{
name: "valid type enums",
e: GenderTypeOther,
b: w,
wantW: strconv.Quote("OTHER"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.e.MarshalGQL(tt.b)

if gotW := w.String(); gotW != tt.wantW {
t.Errorf("GenderType.MarshalGQL() = %v, want %v", gotW, tt.wantW)
}
})
}
}

0 comments on commit e77d9a4

Please sign in to comment.