Skip to content

Commit

Permalink
extract id scalar marshal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Taimoor Ahmad committed Feb 13, 2024
1 parent 4fbe223 commit 5fc925d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Bugfix-20240121-160519.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Bugfix
body: Concrete opslevel.ID structs now correctly JSON marshal to null instead of ""
time: 2024-01-21T16:05:19.350349-05:00
20 changes: 10 additions & 10 deletions scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ func NewID(id ...string) *ID {
return &output
}

func (s ID) GetGraphQLType() string { return "ID" }
func (id ID) GetGraphQLType() string { return "ID" }

func (s *ID) MarshalJSON() ([]byte, error) {
if *s == "" {
func (id ID) MarshalJSON() ([]byte, error) {
if id == "" {
return []byte("null"), nil
}
return []byte(strconv.Quote(string(*s))), nil
return []byte(strconv.Quote(string(id))), nil
}

type Identifier struct {
Id ID `graphql:"id"`
Aliases []string `graphql:"aliases"`
}

func (i IdentifierInput) MarshalJSON() ([]byte, error) {
if i.Id == nil && i.Alias == nil {
func (identifierInput IdentifierInput) MarshalJSON() ([]byte, error) {
if identifierInput.Id == nil && identifierInput.Alias == nil {
return []byte("null"), nil
}
var out string
if i.Id != nil {
out = fmt.Sprintf(`{"id":"%s"}`, string(*i.Id))
if identifierInput.Id != nil {
out = fmt.Sprintf(`{"id":"%s"}`, string(*identifierInput.Id))
} else {
out = fmt.Sprintf(`{"alias":"%s"}`, string(*i.Alias))
out = fmt.Sprintf(`{"alias":"%s"}`, *identifierInput.Alias)
}
return []byte(out), nil
}
Expand All @@ -60,7 +60,7 @@ func NewIdentifier(value ...string) *IdentifierInput {
}

func NewIdentifierArray(values []string) []IdentifierInput {
output := []IdentifierInput{}
output := make([]IdentifierInput, 0)
for _, value := range values {
output = append(output, *NewIdentifier(value))
}
Expand Down
4 changes: 2 additions & 2 deletions scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func TestMarshalID(t *testing.T) {
buf3, err3 := json.Marshal(case3)
// Assert
autopilot.Ok(t, err1)
autopilot.Equals(t, `{"key1":"","key3":null}`, string(buf1))
autopilot.Equals(t, `{"key1":null,"key3":null}`, string(buf1))
autopilot.Ok(t, err2)
autopilot.Equals(t, `{"key1":"","key3":null,"key4":null}`, string(buf2))
autopilot.Equals(t, `{"key1":null,"key3":null,"key4":null}`, string(buf2))
autopilot.Ok(t, err3)
autopilot.Equals(t, `{"key1":"Z2lkOi8vMTIzNDU2Nzg5","key2":"Z2lkOi8vMTIzNDU2Nzg5","key3":"Z2lkOi8vMTIzNDU2Nzg5","key4":"Z2lkOi8vMTIzNDU2Nzg5"}`, string(buf3))
}
Expand Down

0 comments on commit 5fc925d

Please sign in to comment.