debian-forge-composer/internal/osbuild2/curl_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

66 lines
1.4 KiB
Go

package osbuild2
import (
"bytes"
"encoding/json"
)
type CurlSource struct {
Items map[string]CurlSourceItem `json:"items"`
}
func (CurlSource) isSource() {}
// CurlSourceItem can be either a URL string or a URL paired with a secrets
// provider
type CurlSourceItem interface {
isCurlSourceItem()
}
type URL string
func (URL) isCurlSourceItem() {}
type URLWithSecrets struct {
URL string `json:"url"`
Secrets *URLSecrets `json:"secrets,omitempty"`
}
func (URLWithSecrets) isCurlSourceItem() {}
type URLSecrets struct {
Name string `json:"name"`
}
// Unmarshal method for CurlSource for handling the CurlSourceItem interface:
// Tries each of the implementations until it finds the one that works.
func (cs *CurlSource) UnmarshalJSON(data []byte) (err error) {
cs.Items = make(map[string]CurlSourceItem)
type csSimple struct {
Items map[string]URL `json:"items"`
}
simple := new(csSimple)
b := bytes.NewReader(data)
dec := json.NewDecoder(b)
dec.DisallowUnknownFields()
if err = dec.Decode(simple); err == nil {
for k, v := range simple.Items {
cs.Items[k] = v
}
return
}
type csWithSecrets struct {
Items map[string]URLWithSecrets `json:"items"`
}
withSecrets := new(csWithSecrets)
b.Reset(data)
if err = dec.Decode(withSecrets); err == nil {
for k, v := range withSecrets.Items {
cs.Items[k] = v
}
return
}
return
}