Go doesn't really do variants, so we must somehow emulate it. The json objects we use are essentially tagged unions, with a `name` field in reverse domain name notation identifying the type and a type specific 'options' object. In Go we represent this by having an BarOptions interface, which implements a private method `isBarOptions()`, making sure that only types in the same package are able to implement it. Each type FooBar that should belong to the variant implements the interface, and a constructor `NewFooBar(options *FooBarOptions) *Bar` that makes sure the `name` field is set correctly. This would be enough to represent our types and marshal them into JSON, but unmarshalling would not work (json does not know about our tags, so would not know what concrete types to demarshal to). We therefore must also implement the Unmarshall interface for Bar, to select the right types for the Options field. We implement his logic for Target, Stage and Assembler. A handful of concrete types are also implemented, matching what osbuild supports. Signed-off-by: Tom Gundersen <teg@jklm.no>
44 lines
802 B
Go
44 lines
802 B
Go
package target
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
)
|
|
|
|
type Target struct {
|
|
Name string `json:"name"`
|
|
Options TargetOptions `json:"options"`
|
|
}
|
|
|
|
type TargetOptions interface {
|
|
isTargetOptions()
|
|
}
|
|
|
|
type rawTarget struct {
|
|
Name string `json:"name"`
|
|
Options json.RawMessage `json:"options"`
|
|
}
|
|
|
|
func (target *Target) UnmarshalJSON(data []byte) error {
|
|
var rawTarget rawTarget
|
|
err := json.Unmarshal(data, &rawTarget)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var options TargetOptions
|
|
switch rawTarget.Name {
|
|
case "org.osbuild.local":
|
|
options = new(LocalTargetOptions)
|
|
default:
|
|
return errors.New("unexpected target name")
|
|
}
|
|
err = json.Unmarshal(rawTarget.Options, options)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
target.Name = rawTarget.Name
|
|
target.Options = options
|
|
|
|
return nil
|
|
}
|