Skip to content

Instantly share code, notes, and snippets.

@mvantellingen
Created December 18, 2018 12:55
Show Gist options
  • Save mvantellingen/d3fa9e6d78922c3dcd89365abe7b8f88 to your computer and use it in GitHub Desktop.
Save mvantellingen/d3fa9e6d78922c3dcd89365abe7b8f88 to your computer and use it in GitHub Desktop.
func (obj *ScopedPrice) UnmarshalJSON(data []byte) error {
type Alias ScopedPrice
if err := json.Unmarshal(data, (*Alias)(obj)); err != nil {
return err
}
if obj.Value != nil {
obj.Value = mapDiscriminatorTypedMoney(obj.Value)
}
return nil
}
func mapDiscriminatorTypedMoney(input interface{}) TypedMoney {
discriminator := input.(map[string]interface{})["type"].(string)
switch discriminator {
case "centPrecision":
new := CentPrecisionMoney{}
mapstructure.Decode(input, &new)
return new
}
return nil
}
// Insert the type = centPrecision value in the generated JSON
func (obj CentPrecisionMoney) MarshalJSON() ([]byte, error) {
type Alias CentPrecisionMoney
return json.Marshal(struct {
Type string `json:"type"`
*Alias
}{
Type: "centPrecision",
Alias: (*Alias)(&obj)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment