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>
20 lines
391 B
Go
20 lines
391 B
Go
package target
|
|
|
|
type LocalTargetOptions struct {
|
|
Location string `json:"location"`
|
|
}
|
|
|
|
func (LocalTargetOptions) isTargetOptions() {}
|
|
|
|
func NewLocalTargetOptions(location string) *LocalTargetOptions {
|
|
return &LocalTargetOptions{
|
|
Location: location,
|
|
}
|
|
}
|
|
|
|
func NewLocalTarget(options *LocalTargetOptions) *Target {
|
|
return &Target{
|
|
Name: "org.osbuild.local",
|
|
Options: options,
|
|
}
|
|
}
|