The Azure API makes use of Discriminated Types to allow a single API endpoint to provision multiple resources - which are modelled within the Swagger/OpenAPI Definitions as Discriminators.
A field within the Json payload determines which Implementation should be deserialized on both in both the Client (Go SDK) and the Server (Azure API) - as such this field must always be set to a fixed value.
This SDK outputs these as Go Interfaces (parent types) and Go Structs (for implementations) with custom Marshal and Unmarshal functions switching out the correct implementation as required.
For example the Interface (Serialization
) and Implementations (AvroSerialization
and JsonSerialization
) are output in this SDK as:
type Serialization interface {}
var _ Serialization = AvroSerialization{}
type AvroSerialization struct {
Properties *interface{} `json:"properties,omitempty"`
// Fields inherited from Serialization
}
var _ Serialization = JsonSerialization{}
type JsonSerialization struct {
Properties *JsonSerializationProperties `json:"properties,omitempty"`
// Fields inherited from Serialization
}
Given the following Go model, using the Interface defined above:
type Example struct {
Serialization Serialization `json:"serialization"`
}
The desired implementation can be specified directly, for example:
example := Example{
Serialization: JsonSerialization{
// ..
},
}
Given the following Go model, using the Interface defined above:
type Example struct {
Serialization Serialization `json:"serialization"`
}
The specific implementation can be determined via Type Assertion:
// this snippet assumes that the type Example is returned/populated from the SDK
var response Example
if v, ok := response.Serialization.(inputs.AvroSerialization); ok {
// do something with `v` which is an `AvroSerialization`
}
if v, ok := response.Serialization.(inputs.JsonSerialization); ok {
// do something with `v` which is an `JsonSerialization`
}