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

Remove required unmarshal in models #2057

Merged
merged 3 commits into from
Jun 26, 2023
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
123 changes: 62 additions & 61 deletions .generator/poetry.lock

Large diffs are not rendered by default.

49 changes: 19 additions & 30 deletions .generator/src/generator/templates/model_simple.j2
Original file line number Diff line number Diff line change
Expand Up @@ -328,50 +328,35 @@ func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
{%- else %}
func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
raw := map[string]interface{}{}
{%- for attr, spec in model.get("properties", {}).items() if attr in model.get("required", []) %}
{%- set propertyName = attr|attribute_name %}
{%- set dataType = get_type(spec, alternative_name=name + propertyName, render_nullable=True) %}
{%- if loop.first %}
required := struct {
{%- endif %}
{{ propertyName }} {% if not spec.nullable %}*{% endif %}{{ dataType }} `json:"{{ attr }}"`
{%- if loop.last %}
}{}
{%- endif %}
{%- endfor %}
all := struct {
{%- for attr, spec in model.get("properties", {}).items() %}
{%- set propertyName = attr|attribute_name %}
{%- set dataType = get_type(spec, alternative_name=name + propertyName, render_nullable=True) %}
{{ propertyName }} {% if model|is_reference(attr) %}*{% endif %}{{ dataType }} `json:"{{ attr }}{% if attr not in model.get("required", []) %},omitempty{% endif %}"`
{%- endfor %}
}{}
{#- unmarshal required properties #}
{%- for attr, spec in model.get("properties", {}).items() if attr in model.get("required", []) %}
{%- set propertyName = attr|attribute_name %}
{%- if loop.first %}
err = json.Unmarshal(bytes, &required)
if err != nil {
return err
}
{%- endif %}
{%- if not spec.nullable %}
if required.{{ propertyName }} == nil {
{%- if attr in model.get("required", []) %}
{{ propertyName }} {% if not spec.nullable %}*{% endif %}{{ dataType }} `json:"{{ attr }}"`
{%- else %}
if !required.{{ propertyName }}.IsSet() {
{{ propertyName }} {% if model|is_reference(attr) %}*{% endif %}{{ dataType }} `json:"{{ attr }},omitempty"`
{%- endif %}
return fmt.Errorf("required field {{ attr }} missing")
}
{%- endfor %}
err = json.Unmarshal(bytes, &all)
if err != nil {
}{}
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
}
o.UnparsedObject = raw
return nil
}
{%- for attr, spec in model.get("properties", {}).items() if attr in model.get("required", []) %}
{%- set propertyName = attr|attribute_name %}
{%- if not spec.nullable %}
if all.{{ propertyName }} == nil {
{%- else %}
if !all.{{ propertyName }}.IsSet() {
{%- endif %}
return fmt.Errorf("required field {{ attr }} missing")
}
{%- endfor %}

{%- if model.get("additionalProperties", True) %}
additionalProperties := make(map[string]{{ get_type(model.get("additionalProperties", True)) }})
Expand Down Expand Up @@ -407,7 +392,11 @@ func (o *{{ name }}) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
}
{%- endif %}
{%- if attr in model.get("required", []) and not spec.nullable %}
o.{{ propertyName }} = *all.{{ propertyName }}
{%- else %}
o.{{ propertyName }} = all.{{ propertyName }}
{%- endif %}
{%- endfor %}
{%- if model.get("additionalProperties", True) %}
if len(additionalProperties) > 0 {
Expand Down
20 changes: 6 additions & 14 deletions api/datadogV1/model_add_signal_to_incident_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,38 +144,30 @@ func (o AddSignalToIncidentRequest) MarshalJSON() ([]byte, error) {
// UnmarshalJSON deserializes the given payload.
func (o *AddSignalToIncidentRequest) UnmarshalJSON(bytes []byte) (err error) {
raw := map[string]interface{}{}
required := struct {
IncidentId *int64 `json:"incident_id"`
}{}
all := struct {
AddToSignalTimeline *bool `json:"add_to_signal_timeline,omitempty"`
IncidentId int64 `json:"incident_id"`
IncidentId *int64 `json:"incident_id"`
Version *int64 `json:"version,omitempty"`
}{}
err = json.Unmarshal(bytes, &required)
if err != nil {
return err
}
if required.IncidentId == nil {
return fmt.Errorf("required field incident_id missing")
}
err = json.Unmarshal(bytes, &all)
if err != nil {
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
}
o.UnparsedObject = raw
return nil
}
if all.IncidentId == nil {
return fmt.Errorf("required field incident_id missing")
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"add_to_signal_timeline", "incident_id", "version"})
} else {
return err
}
o.AddToSignalTimeline = all.AddToSignalTimeline
o.IncidentId = all.IncidentId
o.IncidentId = *all.IncidentId
o.Version = all.Version
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
Expand Down
50 changes: 20 additions & 30 deletions api/datadogV1/model_alert_graph_widget_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,42 +266,32 @@ func (o AlertGraphWidgetDefinition) MarshalJSON() ([]byte, error) {
// UnmarshalJSON deserializes the given payload.
func (o *AlertGraphWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
raw := map[string]interface{}{}
required := struct {
AlertId *string `json:"alert_id"`
Type *AlertGraphWidgetDefinitionType `json:"type"`
VizType *WidgetVizType `json:"viz_type"`
}{}
all := struct {
AlertId string `json:"alert_id"`
Time *WidgetTime `json:"time,omitempty"`
Title *string `json:"title,omitempty"`
TitleAlign *WidgetTextAlign `json:"title_align,omitempty"`
TitleSize *string `json:"title_size,omitempty"`
Type AlertGraphWidgetDefinitionType `json:"type"`
VizType WidgetVizType `json:"viz_type"`
AlertId *string `json:"alert_id"`
Time *WidgetTime `json:"time,omitempty"`
Title *string `json:"title,omitempty"`
TitleAlign *WidgetTextAlign `json:"title_align,omitempty"`
TitleSize *string `json:"title_size,omitempty"`
Type *AlertGraphWidgetDefinitionType `json:"type"`
VizType *WidgetVizType `json:"viz_type"`
}{}
err = json.Unmarshal(bytes, &required)
if err != nil {
return err
}
if required.AlertId == nil {
return fmt.Errorf("required field alert_id missing")
}
if required.Type == nil {
return fmt.Errorf("required field type missing")
}
if required.VizType == nil {
return fmt.Errorf("required field viz_type missing")
}
err = json.Unmarshal(bytes, &all)
if err != nil {
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
}
o.UnparsedObject = raw
return nil
}
if all.AlertId == nil {
return fmt.Errorf("required field alert_id missing")
}
if all.Type == nil {
return fmt.Errorf("required field type missing")
}
if all.VizType == nil {
return fmt.Errorf("required field viz_type missing")
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"alert_id", "time", "title", "title_align", "title_size", "type", "viz_type"})
Expand Down Expand Up @@ -332,7 +322,7 @@ func (o *AlertGraphWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}
o.AlertId = all.AlertId
o.AlertId = *all.AlertId
if all.Time != nil && all.Time.UnparsedObject != nil && o.UnparsedObject == nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
Expand All @@ -344,8 +334,8 @@ func (o *AlertGraphWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
o.Title = all.Title
o.TitleAlign = all.TitleAlign
o.TitleSize = all.TitleSize
o.Type = all.Type
o.VizType = all.VizType
o.Type = *all.Type
o.VizType = *all.VizType
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}
Expand Down
43 changes: 17 additions & 26 deletions api/datadogV1/model_alert_value_widget_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,39 +305,30 @@ func (o AlertValueWidgetDefinition) MarshalJSON() ([]byte, error) {
// UnmarshalJSON deserializes the given payload.
func (o *AlertValueWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
raw := map[string]interface{}{}
required := struct {
AlertId *string `json:"alert_id"`
Type *AlertValueWidgetDefinitionType `json:"type"`
}{}
all := struct {
AlertId string `json:"alert_id"`
Precision *int64 `json:"precision,omitempty"`
TextAlign *WidgetTextAlign `json:"text_align,omitempty"`
Title *string `json:"title,omitempty"`
TitleAlign *WidgetTextAlign `json:"title_align,omitempty"`
TitleSize *string `json:"title_size,omitempty"`
Type AlertValueWidgetDefinitionType `json:"type"`
Unit *string `json:"unit,omitempty"`
AlertId *string `json:"alert_id"`
Precision *int64 `json:"precision,omitempty"`
TextAlign *WidgetTextAlign `json:"text_align,omitempty"`
Title *string `json:"title,omitempty"`
TitleAlign *WidgetTextAlign `json:"title_align,omitempty"`
TitleSize *string `json:"title_size,omitempty"`
Type *AlertValueWidgetDefinitionType `json:"type"`
Unit *string `json:"unit,omitempty"`
}{}
err = json.Unmarshal(bytes, &required)
if err != nil {
return err
}
if required.AlertId == nil {
return fmt.Errorf("required field alert_id missing")
}
if required.Type == nil {
return fmt.Errorf("required field type missing")
}
err = json.Unmarshal(bytes, &all)
if err != nil {
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
}
o.UnparsedObject = raw
return nil
}
if all.AlertId == nil {
return fmt.Errorf("required field alert_id missing")
}
if all.Type == nil {
return fmt.Errorf("required field type missing")
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"alert_id", "precision", "text_align", "title", "title_align", "title_size", "type", "unit"})
Expand Down Expand Up @@ -368,13 +359,13 @@ func (o *AlertValueWidgetDefinition) UnmarshalJSON(bytes []byte) (err error) {
o.UnparsedObject = raw
return nil
}
o.AlertId = all.AlertId
o.AlertId = *all.AlertId
o.Precision = all.Precision
o.TextAlign = all.TextAlign
o.Title = all.Title
o.TitleAlign = all.TitleAlign
o.TitleSize = all.TitleSize
o.Type = all.Type
o.Type = *all.Type
o.Unit = all.Unit
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
Expand Down
20 changes: 6 additions & 14 deletions api/datadogV1/model_api_error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,27 @@ func (o APIErrorResponse) MarshalJSON() ([]byte, error) {
// UnmarshalJSON deserializes the given payload.
func (o *APIErrorResponse) UnmarshalJSON(bytes []byte) (err error) {
raw := map[string]interface{}{}
required := struct {
Errors *[]string `json:"errors"`
}{}
all := struct {
Errors []string `json:"errors"`
Errors *[]string `json:"errors"`
}{}
err = json.Unmarshal(bytes, &required)
if err != nil {
return err
}
if required.Errors == nil {
return fmt.Errorf("required field errors missing")
}
err = json.Unmarshal(bytes, &all)
if err != nil {
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
}
o.UnparsedObject = raw
return nil
}
if all.Errors == nil {
return fmt.Errorf("required field errors missing")
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"errors"})
} else {
return err
}
o.Errors = all.Errors
o.Errors = *all.Errors
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
}
Expand Down
3 changes: 1 addition & 2 deletions api/datadogV1/model_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ func (o *ApiKey) UnmarshalJSON(bytes []byte) (err error) {
Key *string `json:"key,omitempty"`
Name *string `json:"name,omitempty"`
}{}
err = json.Unmarshal(bytes, &all)
if err != nil {
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions api/datadogV1/model_api_key_list_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func (o *ApiKeyListResponse) UnmarshalJSON(bytes []byte) (err error) {
all := struct {
ApiKeys []ApiKey `json:"api_keys,omitempty"`
}{}
err = json.Unmarshal(bytes, &all)
if err != nil {
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions api/datadogV1/model_api_key_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func (o *ApiKeyResponse) UnmarshalJSON(bytes []byte) (err error) {
all := struct {
ApiKey *ApiKey `json:"api_key,omitempty"`
}{}
err = json.Unmarshal(bytes, &all)
if err != nil {
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
Expand Down
20 changes: 6 additions & 14 deletions api/datadogV1/model_apm_stats_query_column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,31 +177,23 @@ func (o ApmStatsQueryColumnType) MarshalJSON() ([]byte, error) {
// UnmarshalJSON deserializes the given payload.
func (o *ApmStatsQueryColumnType) UnmarshalJSON(bytes []byte) (err error) {
raw := map[string]interface{}{}
required := struct {
Name *string `json:"name"`
}{}
all := struct {
Alias *string `json:"alias,omitempty"`
CellDisplayMode *TableWidgetCellDisplayMode `json:"cell_display_mode,omitempty"`
Name string `json:"name"`
Name *string `json:"name"`
Order *WidgetSort `json:"order,omitempty"`
}{}
err = json.Unmarshal(bytes, &required)
if err != nil {
return err
}
if required.Name == nil {
return fmt.Errorf("required field name missing")
}
err = json.Unmarshal(bytes, &all)
if err != nil {
if err = json.Unmarshal(bytes, &all); err != nil {
err = json.Unmarshal(bytes, &raw)
if err != nil {
return err
}
o.UnparsedObject = raw
return nil
}
if all.Name == nil {
return fmt.Errorf("required field name missing")
}
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
datadog.DeleteKeys(additionalProperties, &[]string{"alias", "cell_display_mode", "name", "order"})
Expand All @@ -226,7 +218,7 @@ func (o *ApmStatsQueryColumnType) UnmarshalJSON(bytes []byte) (err error) {
}
o.Alias = all.Alias
o.CellDisplayMode = all.CellDisplayMode
o.Name = all.Name
o.Name = *all.Name
o.Order = all.Order
if len(additionalProperties) > 0 {
o.AdditionalProperties = additionalProperties
Expand Down
Loading