Skip to content

go: Emit null instead of {} for when struct enum config field is nil #1895

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 0 additions & 5 deletions go/models/empty_map.go

This file was deleted.

3 changes: 1 addition & 2 deletions go/models/ingest_source_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ type IngestSourceInConfig interface {
isIngestSourceInConfig()
}

func (emptyMap) isIngestSourceInConfig() {}
func (CronConfig) isIngestSourceInConfig() {}
func (AdobeSignConfig) isIngestSourceInConfig() {}
func (SvixConfig) isIngestSourceInConfig() {}
Expand Down Expand Up @@ -145,7 +144,7 @@ var IngestSourceInTypeWithNoConfig = map[string]bool{
func (i IngestSourceIn) MarshalJSON() ([]byte, error) {
type Alias IngestSourceIn
if _, found := IngestSourceInTypeWithNoConfig[string(i.Type)]; found {
i.Config = emptyMap{}
i.Config = nil
}
return json.Marshal(&struct{ Alias }{Alias: (Alias)(i)})
}
Expand Down
3 changes: 1 addition & 2 deletions go/models/ingest_source_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ type IngestSourceOutConfig interface {
isIngestSourceOutConfig()
}

func (emptyMap) isIngestSourceOutConfig() {}
func (CronConfig) isIngestSourceOutConfig() {}
func (AdobeSignConfigOut) isIngestSourceOutConfig() {}
func (SvixConfigOut) isIngestSourceOutConfig() {}
Expand Down Expand Up @@ -150,7 +149,7 @@ var IngestSourceOutTypeWithNoConfig = map[string]bool{
func (i IngestSourceOut) MarshalJSON() ([]byte, error) {
type Alias IngestSourceOut
if _, found := IngestSourceOutTypeWithNoConfig[string(i.Type)]; found {
i.Config = emptyMap{}
i.Config = nil
}
return json.Marshal(&struct{ Alias }{Alias: (Alias)(i)})
}
Expand Down
4 changes: 2 additions & 2 deletions go/svix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func TestStructEnumWithFields(t *testing.T) {
}

func TestStructEnumWithNoFields(t *testing.T) {
expectedJson := `{"name":"Mendy","uid":"very unique","type":"generic-webhook","config":{}}`
expectedJson := `{"name":"Mendy","uid":"very unique","type":"generic-webhook","config":null}`
uid := "very unique"
sourceIn := models.IngestSourceIn{
Name: "Mendy",
Expand All @@ -702,7 +702,7 @@ func TestStructEnumWithNoFields(t *testing.T) {
}

func TestStructEnumWithNoFieldsNilConfig(t *testing.T) {
expectedJson := `{"name":"Mendy","uid":"very unique","type":"generic-webhook","config":{}}`
expectedJson := `{"name":"Mendy","uid":"very unique","type":"generic-webhook","config":null}`
uid := "very unique"
sourceIn := models.IngestSourceIn{
Name: "Mendy",
Expand Down
4 changes: 1 addition & 3 deletions openapi-templates/go/types/struct_enum.go.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ type {{ ty_name }}{{ content_field }} interface {
{% for schema_ref in type.variants| map(attribute="schema_ref") | unique -%}
{% if schema_ref is defined -%}
func ({{ schema_ref | to_upper_camel_case }}) is{{ ty_name }}{{ content_field }}(){}
{% else -%}
func (emptyMap) is{{ ty_name }}{{ content_field }}(){}
{% endif -%}
{% endfor %}

Expand Down Expand Up @@ -93,7 +91,7 @@ func (i {{ ty_name }}) MarshalJSON() ([]byte, error) {
type Alias {{ ty_name }}
{% if has_variant_with_no_schema_ref -%}
if _, found := {{ ty_name }}{{ discriminator_field }}WithNo{{ content_field }}[string(i.{{ discriminator_field }})]; found {
i.{{ content_field }} = emptyMap{}
i.{{ content_field }} = nil
}
{% endif -%}
return json.Marshal(&struct {Alias}{Alias: (Alias)(i)})
Expand Down