Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove omitempty json tag for required field #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ func (g *generator) convertType(
// Type is a list.
elem, err := g.convertType(
namePrefix, typ.Elem, selectionSet, options, queryOptions)

// [String!]! -> `json:"xxx"`
// [String!] -> `json:"xxx,omitempty"`
if !typ.NonNull {
oe := true
options.Omitempty = &oe
}

return &goSliceType{elem}, err
}

Expand All @@ -258,6 +266,14 @@ func (g *generator) convertType(
// Note this does []*T or [][]*T, not e.g. *[][]T. See #16.
goTyp = &goPointerType{goTyp}
}

// String! -> `json:"xxx"`
// String -> `json:"xxx,omitempty"`
if !typ.NonNull {
oe := true
options.Omitempty = &oe
}

return goTyp, err
}

Expand Down
12 changes: 10 additions & 2 deletions generate/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,21 @@ func expandFilenames(globs []string) ([]string, error) {
if err != nil {
return nil, errorf(nil, "can't expand file-glob %v: %v", glob, err)
}
if len(matches) == 0 {
return nil, errorf(nil, "%v did not match any files", glob)
}
for _, match := range matches {
uniqFilenames[match] = true
uniqFilenames[match] = false
}
}
// item should keep same order with original globs
filenames := make([]string, 0, len(uniqFilenames))
for filename := range uniqFilenames {
for _, filename := range globs {
if uniqFilenames[filename] {
continue
}
filenames = append(filenames, filename)
uniqFilenames[filename] = true
}
return filenames, nil
}
Expand Down