Skip to content
Open
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
14 changes: 13 additions & 1 deletion templates/go/models/model.go.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,24 @@ func (model {{ definition.name | caseUcfirst }}) New(data []byte) *{{ definition
return &model
}

{%~ if definition.additionalProperties %}
func (p *{{ definition.name | caseUcfirst }}) UnmarshalJSON(b []byte) error {
p.data = make([]byte, len(b))
copy(p.data, b)

// Alias avoids infinite recursion (the alias has no methods).
type Alias {{ definition.name | caseUcfirst }}
aux := (*Alias)(p)
return json.Unmarshal(b, aux)
}
{%~ endif %}

{%~ if definition.additionalProperties %}
// Use this method to get response in desired type
{%~ endif %}
func (model *{{ definition.name | caseUcfirst }}) Decode(value interface{}) error {
if len(model.data) <= 0 {
return errors.New("method Decode() cannot be used on nested struct")
return errors.New("model must be initialized from raw JSON via New() or JSON unmarshalling")
}

err := json.Unmarshal(model.data, value)
Expand Down