debian-forge-composer/internal/osbuild2/source.go
Achilleas Koutsou 756d5b063f osbuild2: new schema types: stages, inputs, sources
Adding new types and adapting copies of all the old types to match the
new Manifest schema:

New types:
- Stages
    - org.osbuild.ostree.init
    - org.osbuild.ostree.pull
    - org.osbuild.ostree.preptree (replaces org.osbuild.rpm-ostree)
    - org.osbuild.curl
- Converted from assemblers
    The concept of a Build and Assembler stage in gone now. Instead they
    are regular Stages like any other.
    - org.osbuild.oci-archive
    - org.osbuild.ostree.commit
- Sources
    - org.osbuild.curl
    - org.osbuild.ostree
- Inputs
    - org.osbuild.files
    - org.osbuild.ostree

Types with changes:
- Stages
    - org.osbuild.rpm:
        - New input structure for defining packages
        - New options

Basically copies:
- The rest simply rename the `Name` field to `Type`

Decoding types with interface fields:
Types that contain interfaces with multiple implementations implement
their own UnmarshalJSON method.  In these cases, we use a JSON decoder
with the `DisallowUnknownFields` option to catch errors during the
deserialization while trying to determine which implementation matches
the data.

Copied tests for copied types are adapted accordingly.
2021-03-17 18:12:17 +00:00

49 lines
1.1 KiB
Go

package osbuild2
import (
"encoding/json"
"errors"
)
// A Sources map contains all the sources made available to an osbuild run
type Sources map[string]Source
// Source specifies the operations of a given source-type.
type Source interface {
isSource()
}
type SourceOptions interface {
isSourceOptions()
}
type rawSources map[string]json.RawMessage
// UnmarshalJSON unmarshals JSON into a Source object. Each type of source has
// a custom unmarshaller for its options, selected based on the source name.
func (sources *Sources) UnmarshalJSON(data []byte) error {
var rawSources rawSources
err := json.Unmarshal(data, &rawSources)
if err != nil {
return err
}
*sources = make(map[string]Source)
for name, rawSource := range rawSources {
var source Source
switch name {
case "org.osbuild.curl":
source = new(CurlSource)
case "org.osbuild.ostree":
source = new(OSTreeSource)
default:
return errors.New("unexpected source name: " + name)
}
err = json.Unmarshal(rawSource, source)
if err != nil {
return err
}
(*sources)[name] = source
}
return nil
}