Skip to content

Conversation

sonatard
Copy link
Contributor

@sonatard sonatard commented Apr 13, 2025

Starting with Go 1.24, support for omitzero was added, so it can now be enabled as an option.

Add enable_model_json_omitzero_tag flag. Currently, the enable_model_json_omitzero_tag default is false for backward compatibility. However, the json v2 package recommends using omitzero instead of omitempty. The specification for omitempty changed significantly in v2, so migrating to omitzero seems reasonable.

In the future, when gqlgen supports the json v2 package, change the default of enable_model_json_omitzero_tag to true and the default of enable_model_json_omitempty_tag to false.

  • omitzero
enable_model_json_omitempty_tag: false
enable_model_json_omitzero_tag: true
type OmitEmptyJSONTagTest struct {
	ValueNonNil string  `json:"ValueNonNil" database:"OmitEmptyJsonTagTestValueNonNil"`
	Value       *string `json:"Value,omitzero" database:"OmitEmptyJsonTagTestValue"`
}
  • omitzero and omitempty
enable_model_json_omitempty_tag: true # or not set
enable_model_json_omitzero_tag: true
type OmitEmptyJSONTagTest struct {
	ValueNonNil string  `json:"ValueNonNil" database:"OmitEmptyJsonTagTestValueNonNil"`
	Value       *string `json:"Value,omitempty,omitzero" database:"OmitEmptyJsonTagTestValue"`
}
  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)

@coveralls
Copy link

coveralls commented Apr 13, 2025

Coverage Status

coverage: 73.328% (-0.003%) from 73.331%
when pulling 1a3f08f on sonatard:go124-omitzero
into 55536f4 on 99designs:master.

@sonatard sonatard force-pushed the go124-omitzero branch 3 times, most recently from e054209 to 949c754 Compare April 13, 2025 08:35
@sonatard sonatard changed the title Update Go 1.24 and Support omitzero Update Go 1.24 and support omitzero Apr 13, 2025
@sonatard sonatard marked this pull request as ready for review April 13, 2025 08:42
@sonatard sonatard changed the title Update Go 1.24 and support omitzero Support omitzero Apr 14, 2025
@StevenACoffman
Copy link
Collaborator

Thanks!

@StevenACoffman StevenACoffman merged commit f207e08 into 99designs:master Apr 14, 2025
17 checks passed
@sonatard sonatard deleted the go124-omitzero branch April 14, 2025 02:14
@sonatard sonatard mentioned this pull request Apr 22, 2025
40 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants