From f74679b3b6eabb5a12e971a41d8e5d09ab0aa278 Mon Sep 17 00:00:00 2001 From: Vlad Barosan Date: Tue, 6 Mar 2018 11:36:25 -0800 Subject: [PATCH 1/2] Generate marshallers if there are flattened fields --- src/Templates/ModelTemplate.cshtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Templates/ModelTemplate.cshtml b/src/Templates/ModelTemplate.cshtml index 05c2c3e2f..cf315dab9 100644 --- a/src/Templates/ModelTemplate.cshtml +++ b/src/Templates/ModelTemplate.cshtml @@ -141,7 +141,7 @@ else } -@if (Model.BaseIsPolymorphic || Model.IsPolymorphic || Model.AllProperties.Any(p => p.ModelType is DictionaryTypeGo)) +@if (Model.BaseIsPolymorphic || Model.IsPolymorphic || Model.HasFlattenedFields || Model.AllProperties.Any(p => p.ModelType is DictionaryTypeGo)) { @EmptyLine @@ -319,7 +319,7 @@ else { @:@(Model.Name.FixedValue.ToShortName()).@(p.Name) = @(CodeNamerGo.Instance.GetVariableName(p.Name)) } - else + else { @:@(Model.Name.FixedValue.ToShortName()).@(p.Name) = &@(CodeNamerGo.Instance.GetVariableName(p.Name)) } From a2414470c7bd597b96628a21fe252109a5c986f1 Mon Sep 17 00:00:00 2001 From: Vlad Barosan Date: Tue, 6 Mar 2018 11:38:14 -0800 Subject: [PATCH 2/2] regenerate tests --- test/src/tests/generated/lro/models.go | 12 +++++ .../generated/model-flattening/models.go | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/test/src/tests/generated/lro/models.go b/test/src/tests/generated/lro/models.go index 6cd2496cf..e6b5cc09b 100644 --- a/test/src/tests/generated/lro/models.go +++ b/test/src/tests/generated/lro/models.go @@ -3854,6 +3854,18 @@ type SubProduct struct { ID *string `json:"id,omitempty"` } +// MarshalJSON is the custom marshaler for SubProduct. +func (sp SubProduct) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]interface{}) + if sp.SubProductProperties != nil { + objectMap["properties"] = sp.SubProductProperties + } + if sp.ID != nil { + objectMap["id"] = sp.ID + } + return json.Marshal(objectMap) +} + // UnmarshalJSON is the custom unmarshaler for SubProduct struct. func (sp *SubProduct) UnmarshalJSON(body []byte) error { var m map[string]*json.RawMessage diff --git a/test/src/tests/generated/model-flattening/models.go b/test/src/tests/generated/model-flattening/models.go index bc9edd70a..95856e5fc 100644 --- a/test/src/tests/generated/model-flattening/models.go +++ b/test/src/tests/generated/model-flattening/models.go @@ -54,6 +54,21 @@ type Error struct { *Error `json:"parentError,omitempty"` } +// MarshalJSON is the custom marshaler for Error. +func (e Error) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]interface{}) + if e.Status != nil { + objectMap["status"] = e.Status + } + if e.Message != nil { + objectMap["message"] = e.Message + } + if e.Error != nil { + objectMap["parentError"] = e.Error + } + return json.Marshal(objectMap) +} + // UnmarshalJSON is the custom unmarshaler for Error struct. func (e *Error) UnmarshalJSON(body []byte) error { var m map[string]*json.RawMessage @@ -243,6 +258,15 @@ type ProductWrapper struct { *WrappedProduct `json:"property,omitempty"` } +// MarshalJSON is the custom marshaler for ProductWrapper. +func (pw ProductWrapper) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]interface{}) + if pw.WrappedProduct != nil { + objectMap["property"] = pw.WrappedProduct + } + return json.Marshal(objectMap) +} + // UnmarshalJSON is the custom unmarshaler for ProductWrapper struct. func (pw *ProductWrapper) UnmarshalJSON(body []byte) error { var m map[string]*json.RawMessage @@ -349,6 +373,21 @@ type SimpleProduct struct { Description *string `json:"base_product_description,omitempty"` } +// MarshalJSON is the custom marshaler for SimpleProduct. +func (sp SimpleProduct) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]interface{}) + if sp.SimpleProductProperties != nil { + objectMap["details"] = sp.SimpleProductProperties + } + if sp.ProductID != nil { + objectMap["base_product_id"] = sp.ProductID + } + if sp.Description != nil { + objectMap["base_product_description"] = sp.Description + } + return json.Marshal(objectMap) +} + // UnmarshalJSON is the custom unmarshaler for SimpleProduct struct. func (sp *SimpleProduct) UnmarshalJSON(body []byte) error { var m map[string]*json.RawMessage @@ -400,6 +439,21 @@ type SimpleProductProperties struct { *ProductURL `json:"max_product_image,omitempty"` } +// MarshalJSON is the custom marshaler for SimpleProductProperties. +func (spp SimpleProductProperties) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]interface{}) + if spp.MaxProductDisplayName != nil { + objectMap["max_product_display_name"] = spp.MaxProductDisplayName + } + if spp.Capacity != nil { + objectMap["max_product_capacity"] = spp.Capacity + } + if spp.ProductURL != nil { + objectMap["max_product_image"] = spp.ProductURL + } + return json.Marshal(objectMap) +} + // UnmarshalJSON is the custom unmarshaler for SimpleProductProperties struct. func (spp *SimpleProductProperties) UnmarshalJSON(body []byte) error { var m map[string]*json.RawMessage