Skip to content

Commit

Permalink
[Go][Experimental] Fix discriminator lookup (#6521)
Browse files Browse the repository at this point in the history
* bug fix, better code format

* update oas3 sample
  • Loading branch information
wing328 authored Jun 2, 2020
1 parent 8fc7ec8 commit 4d68953
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,40 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
}

{{/isNullable}}
{{#useOneOfDiscriminatorLookup}}
{{#discriminator}}
{{#mappedModels}}
{{#-first}}
// use discriminator value to speed up the lookup
var jsonDict map[string]interface{}
err := json.Unmarshal(data, &jsonDict)
if err != nil {
return fmt.Errorf("Failed to unmarshal JSON into map for the discrimintor lookup.")
}
{{#useOneOfDiscriminatorLookup}}
{{#discriminator}}
{{#mappedModels}}
{{#-first}}
// use discriminator value to speed up the lookup
var jsonDict map[string]interface{}
err = json.Unmarshal(data, &jsonDict)
if err != nil {
return fmt.Errorf("Failed to unmarshal JSON into map for the discrimintor lookup.")
}

{{/-first}}
// check if the discriminator value is '{{{mappingName}}}'
if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" {
// try to unmarshal JSON data into {{{modelName}}}
err = json.Unmarshal(data, &dst.{{{modelName}}});
if err == nil {
json{{{modelName}}}, _ := json.Marshal(dst.{{{modelName}}})
if string(json{{{modelName}}}) == "{}" { // empty struct
dst.{{{modelName}}} = nil
} else {
return nil // data stored in dst.{{{modelName}}}, return on the first match
}
} else {
dst.{{{modelName}}} = nil
}
}
{{/-first}}
// check if the discriminator value is '{{{mappingName}}}'
if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" {
// try to unmarshal JSON data into {{{modelName}}}
err = json.Unmarshal(data, &dst.{{{modelName}}})
if err == nil {
json{{{modelName}}}, _ := json.Marshal(dst.{{{modelName}}})
if string(json{{{modelName}}}) == "{}" { // empty struct
dst.{{{modelName}}} = nil
} else {
return nil // data stored in dst.{{{modelName}}}, return on the first match
}
} else {
dst.{{{modelName}}} = nil
}
}

{{/mappedModels}}
{{/discriminator}}
{{/useOneOfDiscriminatorLookup}}
{{/mappedModels}}
{{/discriminator}}
{{/useOneOfDiscriminatorLookup}}
{{#oneOf}}
// try to unmarshal data into {{{.}}}
err = json.Unmarshal(data, &dst.{{{.}}});
err = json.Unmarshal(data, &dst.{{{.}}})
if err == nil {
json{{{.}}}, _ := json.Marshal(dst.{{{.}}})
if string(json{{{.}}}) == "{}" { // empty struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (dst *Fruit) UnmarshalJSON(data []byte) error {
var err error
match := 0
// try to unmarshal data into Apple
err = json.Unmarshal(data, &dst.Apple);
err = json.Unmarshal(data, &dst.Apple)
if err == nil {
jsonApple, _ := json.Marshal(dst.Apple)
if string(jsonApple) == "{}" { // empty struct
Expand All @@ -49,7 +49,7 @@ func (dst *Fruit) UnmarshalJSON(data []byte) error {
}

// try to unmarshal data into Banana
err = json.Unmarshal(data, &dst.Banana);
err = json.Unmarshal(data, &dst.Banana)
if err == nil {
jsonBanana, _ := json.Marshal(dst.Banana)
if string(jsonBanana) == "{}" { // empty struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (dst *FruitReq) UnmarshalJSON(data []byte) error {
var err error
match := 0
// try to unmarshal data into AppleReq
err = json.Unmarshal(data, &dst.AppleReq);
err = json.Unmarshal(data, &dst.AppleReq)
if err == nil {
jsonAppleReq, _ := json.Marshal(dst.AppleReq)
if string(jsonAppleReq) == "{}" { // empty struct
Expand All @@ -49,7 +49,7 @@ func (dst *FruitReq) UnmarshalJSON(data []byte) error {
}

// try to unmarshal data into BananaReq
err = json.Unmarshal(data, &dst.BananaReq);
err = json.Unmarshal(data, &dst.BananaReq)
if err == nil {
jsonBananaReq, _ := json.Marshal(dst.BananaReq)
if string(jsonBananaReq) == "{}" { // empty struct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (dst *Mammal) UnmarshalJSON(data []byte) error {
var err error
match := 0
// try to unmarshal data into Whale
err = json.Unmarshal(data, &dst.Whale);
err = json.Unmarshal(data, &dst.Whale)
if err == nil {
jsonWhale, _ := json.Marshal(dst.Whale)
if string(jsonWhale) == "{}" { // empty struct
Expand All @@ -49,7 +49,7 @@ func (dst *Mammal) UnmarshalJSON(data []byte) error {
}

// try to unmarshal data into Zebra
err = json.Unmarshal(data, &dst.Zebra);
err = json.Unmarshal(data, &dst.Zebra)
if err == nil {
jsonZebra, _ := json.Marshal(dst.Zebra)
if string(jsonZebra) == "{}" { // empty struct
Expand Down

0 comments on commit 4d68953

Please sign in to comment.