Misc Export fixes

- Temporarily disable export of base.cue as it causes merge errors at
  the end of compute
- Fixes for JSON export for Scalar and Lists
- Add YAML export
- Removed BOOL and NUMBER support, using JSON for now
- Re-enabled all export tests

Fixes #36

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-01-25 15:07:54 -08:00
parent f8a7722261
commit c6e010d4f0
11 changed files with 189 additions and 22 deletions

View File

@@ -325,8 +325,6 @@ func (env *Env) Walk(ctx context.Context, fn EnvWalkFunc) (*Value, error) {
lg.Debug().Msg("cueflow task: filling result")
// Merge task value into output
var err error
// FIXME: does cueflow.Task.Value() contain only filled values,
// or base + filled?
out, err = out.MergePath(t.Value(), t.Path())
if err != nil {
lg.

View File

@@ -42,9 +42,8 @@ package dagger
#dagger: #ComponentConfig
...
} | {
// Match embedded strings
// FIXME: match all embedded scalar types
string
// Match embedded scalars
bool | int | float | string | bytes
#dagger: #ComponentConfig
}
@@ -68,7 +67,7 @@ package dagger
do: "export"
// Source path in the container
source: string
format: "json" | "yaml" | *"string" | "number" | "boolean"
format: "json" | "yaml" | *"string"
}
#Local: {

View File

@@ -8,6 +8,7 @@ import (
"github.com/moby/buildkit/client/llb"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
)
type Op struct {
@@ -237,7 +238,8 @@ func (op *Op) Export(ctx context.Context, fs FS, out *Fillable) (FS, error) {
}
case "json":
var o interface{}
if err := json.Unmarshal(contents, &o); err != nil {
o, err := unmarshalAnything(contents, json.Unmarshal)
if err != nil {
return fs, err
}
@@ -247,6 +249,22 @@ func (op *Op) Export(ctx context.Context, fs FS, out *Fillable) (FS, error) {
Interface("contents", o).
Msg("exporting json")
if err := out.Fill(o); err != nil {
return fs, err
}
case "yaml":
var o interface{}
o, err := unmarshalAnything(contents, yaml.Unmarshal)
if err != nil {
return fs, err
}
log.
Ctx(ctx).
Debug().
Interface("contents", o).
Msg("exporting yaml")
if err := out.Fill(o); err != nil {
return fs, err
}
@@ -256,6 +274,25 @@ func (op *Op) Export(ctx context.Context, fs FS, out *Fillable) (FS, error) {
return fs, nil
}
type unmarshaller func([]byte, interface{}) error
func unmarshalAnything(data []byte, fn unmarshaller) (interface{}, error) {
// unmarshalling a map into interface{} yields an error:
// "unsupported Go type for map key (interface {})"
// we want to attempt to unmarshal to a map[string]interface{} first
var oMap map[string]interface{}
if err := fn(data, &oMap); err == nil {
return oMap, nil
}
// If the previous attempt didn't work, we might be facing a scalar (e.g.
// bool).
// Try to unmarshal to interface{} directly.
var o interface{}
err := fn(data, &o)
return o, err
}
func (op *Op) Load(ctx context.Context, fs FS, out *Fillable) (FS, error) {
from, err := newExecutable(op.Get("from"))
if err != nil {

View File

@@ -37,9 +37,8 @@ package dagger
#dagger: #ComponentConfig
...
} | {
// Match embedded strings
// FIXME: match all embedded scalar types
string
// Match embedded scalars
bool | int | float | string | bytes
#dagger: #ComponentConfig
}
@@ -63,7 +62,7 @@ package dagger
do: "export"
// Source path in the container
source: string
format: "json" | "yaml" | *"string" | "number" | "boolean"
format: "json" | "yaml" | *"string"
}
#Local: {