debian-forge-composer/internal/pipeline/assembler.go
Tom Gundersen 09ac84926e blueprint: add unit tests for ex4 output type
Also fix a bug where the RawFs assembler was not correctly unmarshaled.

Signed-off-by: Tom Gundersen <teg@jklm.no>
2019-10-21 23:17:49 +02:00

53 lines
1.3 KiB
Go

package pipeline
import (
"encoding/json"
"errors"
)
// An Assembler turns a filesystem tree into a target image.
type Assembler struct {
Name string `json:"name"`
Options AssemblerOptions `json:"options"`
}
// AssemblerOptions specify the operations of a given assembler-type.
type AssemblerOptions interface {
isAssemblerOptions()
}
type rawAssembler struct {
Name string `json:"name"`
Options json.RawMessage `json:"options"`
}
// UnmarshalJSON unmarshals JSON into an Assembler object. Each type of
// assembler has a custom unmarshaller for its options, selected based on the
// stage name.
func (assembler *Assembler) UnmarshalJSON(data []byte) error {
var rawAssembler rawAssembler
err := json.Unmarshal(data, &rawAssembler)
if err != nil {
return err
}
var options AssemblerOptions
switch rawAssembler.Name {
case "org.osbuild.tar":
options = new(TarAssemblerOptions)
case "org.osbuild.qemu":
options = new(QEMUAssemblerOptions)
case "org.osbuild.rawfs":
options = new(RawFSAssemblerOptions)
default:
return errors.New("unexpected assembler name")
}
err = json.Unmarshal(rawAssembler.Options, options)
if err != nil {
return err
}
assembler.Name = rawAssembler.Name
assembler.Options = options
return nil
}