diff --git a/enums.go b/enums.go index 0e6b4f5..f2ab591 100644 --- a/enums.go +++ b/enums.go @@ -10,6 +10,8 @@ type IdentifierType string type ContactType string +type GenderType string + const ( // Identifier types IdentifierTypeNationalID IdentifierType = "NATIONAL_ID" @@ -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 { @@ -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())) +} diff --git a/enums_test.go b/enums_test.go index ff85af2..efa8619 100644 --- a/enums_test.go +++ b/enums_test.go @@ -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) + } + }) + } +}