compiler cleanup

Now using the same pattern as Go's http package.

- the `compiler.Compiler` struct can be used directly (and tests to do
  to avoid messing with the global version)
- `compiler.DefaultCompiler` contains a public default Compiler instance
- `compiler` exposes proxy functions (e.g. Compile) back to the
  DefaultCompiler

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-02-16 18:31:03 -08:00
parent eb0c0a9375
commit 74e084944e
14 changed files with 195 additions and 192 deletions

View File

@@ -0,0 +1,91 @@
package compiler
import (
"errors"
"sync"
"cuelang.org/go/cue"
cueerrors "cuelang.org/go/cue/errors"
)
var (
// DefaultCompiler is the default Compiler and is used by Compile
DefaultCompiler = &Compiler{}
)
func Compile(name string, src interface{}) (*Value, error) {
return DefaultCompiler.Compile(name, src)
}
func EmptyStruct() (*Value, error) {
return DefaultCompiler.EmptyStruct()
}
// FIXME can be refactored away now?
func Wrap(v cue.Value, inst *cue.Instance) *Value {
return DefaultCompiler.Wrap(v, inst)
}
func Cue() *cue.Runtime {
return DefaultCompiler.Cue()
}
func Err(err error) error {
return DefaultCompiler.Err(err)
}
// Polyfill for a cue runtime
// (we call it compiler to avoid confusion with dagger runtime)
// Use this instead of cue.Runtime
type Compiler struct {
l sync.RWMutex
cue.Runtime
}
func (c *Compiler) lock() {
c.l.Lock()
}
func (c *Compiler) unlock() {
c.l.Unlock()
}
func (c *Compiler) rlock() {
c.l.RLock()
}
func (c *Compiler) runlock() {
c.l.RUnlock()
}
func (c *Compiler) Cue() *cue.Runtime {
return &(c.Runtime)
}
// Compile an empty struct
func (c *Compiler) EmptyStruct() (*Value, error) {
return c.Compile("", "")
}
func (c *Compiler) Compile(name string, src interface{}) (*Value, error) {
c.lock()
defer c.unlock()
inst, err := c.Cue().Compile(name, src)
if err != nil {
// FIXME: cleaner way to unwrap cue error details?
return nil, Err(err)
}
return c.Wrap(inst.Value(), inst), nil
}
func (c *Compiler) Wrap(v cue.Value, inst *cue.Instance) *Value {
return wrapValue(v, inst, c)
}
func (c *Compiler) Err(err error) error {
if err == nil {
return nil
}
return errors.New(cueerrors.Details(err, &cueerrors.Config{}))
}

View File

@@ -0,0 +1,64 @@
package compiler
import (
"testing"
)
// Test that a non-existing field is detected correctly
func TestFieldNotExist(t *testing.T) {
c := &Compiler{}
root, err := c.Compile("test.cue", `foo: "bar"`)
if err != nil {
t.Fatal(err)
}
if v := root.Get("foo"); !v.Exists() {
// value should exist
t.Fatal(v)
}
if v := root.Get("bar"); v.Exists() {
// value should NOT exist
t.Fatal(v)
}
}
// Test that a non-existing definition is detected correctly
func TestDefNotExist(t *testing.T) {
c := &Compiler{}
root, err := c.Compile("test.cue", `foo: #bla: "bar"`)
if err != nil {
t.Fatal(err)
}
if v := root.Get("foo.#bla"); !v.Exists() {
// value should exist
t.Fatal(v)
}
if v := root.Get("foo.#nope"); v.Exists() {
// value should NOT exist
t.Fatal(v)
}
}
func TestSimple(t *testing.T) {
c := &Compiler{}
_, err := c.EmptyStruct()
if err != nil {
t.Fatal(err)
}
}
func TestJSON(t *testing.T) {
c := &Compiler{}
v, err := c.Compile("", `foo: hello: "world"`)
if err != nil {
t.Fatal(err)
}
b1 := v.JSON()
if string(b1) != `{"foo":{"hello":"world"}}` {
t.Fatal(b1)
}
// Reproduce a bug where Value.Get().JSON() ignores Get()
b2 := v.Get("foo").JSON()
if string(b2) != `{"hello":"world"}` {
t.Fatal(b2)
}
}

137
dagger/compiler/json.go Normal file
View File

@@ -0,0 +1,137 @@
package compiler
import (
"fmt"
"cuelang.org/go/cue"
cuejson "cuelang.org/go/encoding/json"
"github.com/KromDaniel/jonson"
)
type JSON []byte
func (s JSON) Get(path ...string) ([]byte, error) {
if s == nil {
s = []byte("{}")
}
var (
root *jonson.JSON
)
root, err := jonson.Parse(s)
if err != nil {
return nil, fmt.Errorf("parse root json: %w", err)
}
pointer := root
for _, key := range path {
// FIXME: we can traverse maps but not arrays (need to handle int keys)
pointer = pointer.At(key)
}
// FIXME: use indent function from stdlib
return pointer.ToJSON()
}
func (s JSON) Unset(path ...string) (JSON, error) {
if s == nil {
s = []byte("{}")
}
var (
root *jonson.JSON
)
root, err := jonson.Parse(s)
if err != nil {
return nil, fmt.Errorf("unset: parse root json: %w", err)
}
var (
pointer = root
pathDir []string
)
if len(path) > 0 {
pathDir = path[:len(path)-1]
}
for _, key := range pathDir {
pointer = pointer.At(key)
}
if len(path) == 0 {
pointer.Set(nil)
} else {
key := path[len(path)-1]
pointer.DeleteMapKey(key)
}
return root.ToJSON()
}
func (s JSON) Set(valueJSON []byte, path ...string) (JSON, error) {
if s == nil {
s = []byte("{}")
}
var (
root *jonson.JSON
value *jonson.JSON
)
root, err := jonson.Parse(s)
if err != nil {
return nil, fmt.Errorf("parse root json: %w", err)
}
value, err = jonson.Parse(valueJSON)
if err != nil {
return nil, fmt.Errorf("SetJSON: parse value json: |%s|: %w", valueJSON, err)
}
var (
pointer = root
pathDir []string
)
if len(path) > 0 {
pathDir = path[:len(path)-1]
}
for _, key := range pathDir {
if !pointer.ObjectKeyExists(key) {
pointer.MapSet(key, jonson.NewEmptyJSONMap())
}
pointer = pointer.At(key)
}
if len(path) == 0 {
pointer.Set(value)
} else {
key := path[len(path)-1]
pointer.MapSet(key, value)
}
return root.ToJSON()
}
func (s JSON) Merge(layers ...JSON) (JSON, error) {
r := new(cue.Runtime)
var resultInst *cue.Instance
for i, l := range append([]JSON{s}, layers...) {
if l == nil {
continue
}
filename := fmt.Sprintf("%d", i)
inst, err := cuejson.Decode(r, filename, []byte(l))
if err != nil {
return nil, err
}
if resultInst == nil {
resultInst = inst
} else {
resultInst, err = resultInst.Fill(inst.Value())
if err != nil {
return nil, err
}
if resultInst.Err != nil {
return nil, resultInst.Err
}
}
}
b, err := resultInst.Value().MarshalJSON()
if err != nil {
return nil, err
}
return JSON(b), nil
}
func (s JSON) String() string {
if s == nil {
return "{}"
}
return string(s)
}

22
dagger/compiler/utils.go Normal file
View File

@@ -0,0 +1,22 @@
package compiler
import (
"cuelang.org/go/cue"
)
func cueStringsToCuePath(parts ...string) cue.Path {
selectors := make([]cue.Selector, 0, len(parts))
for _, part := range parts {
selectors = append(selectors, cue.Str(part))
}
return cue.MakePath(selectors...)
}
func cuePathToStrings(p cue.Path) []string {
selectors := p.Selectors()
out := make([]string, len(selectors))
for i, sel := range selectors {
out[i] = sel.String()
}
return out
}

248
dagger/compiler/value.go Normal file
View File

@@ -0,0 +1,248 @@
package compiler
import (
"cuelang.org/go/cue"
cueformat "cuelang.org/go/cue/format"
)
// Value is a wrapper around cue.Value.
// Use instead of cue.Value and cue.Instance
type Value struct {
val cue.Value
cc *Compiler
inst *cue.Instance
}
func (v *Value) CueInst() *cue.Instance {
return v.inst
}
func (v *Value) Wrap(v2 cue.Value) *Value {
return wrapValue(v2, v.inst, v.cc)
}
func wrapValue(v cue.Value, inst *cue.Instance, cc *Compiler) *Value {
return &Value{
val: v,
cc: cc,
inst: inst,
}
}
// Fill the value in-place, unlike Merge which returns a copy.
func (v *Value) Fill(x interface{}) error {
v.cc.lock()
defer v.cc.unlock()
// If calling Fill() with a Value, we want to use the underlying
// cue.Value to fill.
if val, ok := x.(*Value); ok {
v.val = v.val.Fill(val.val)
} else {
v.val = v.val.Fill(x)
}
return v.Validate()
}
// LookupPath is a concurrency safe wrapper around cue.Value.LookupPath
func (v *Value) LookupPath(p cue.Path) *Value {
v.cc.rlock()
defer v.cc.runlock()
return v.Wrap(v.val.LookupPath(p))
}
// Lookup is a helper function to lookup by path parts.
func (v *Value) Lookup(path ...string) *Value {
return v.LookupPath(cueStringsToCuePath(path...))
}
// Get is a helper function to lookup by path string
func (v *Value) Get(target string) *Value {
return v.LookupPath(cue.ParsePath(target))
}
// Proxy function to the underlying cue.Value
func (v *Value) Len() cue.Value {
return v.val.Len()
}
// Proxy function to the underlying cue.Value
func (v *Value) Fields() (*cue.Iterator, error) {
return v.val.Fields()
}
// Proxy function to the underlying cue.Value
func (v *Value) Struct() (*cue.Struct, error) {
return v.val.Struct()
}
// Proxy function to the underlying cue.Value
func (v *Value) Exists() bool {
return v.val.Exists()
}
// Proxy function to the underlying cue.Value
func (v *Value) String() (string, error) {
return v.val.String()
}
func (v *Value) SourceUnsafe() string {
s, _ := v.SourceString()
return s
}
// Proxy function to the underlying cue.Value
func (v *Value) Path() cue.Path {
return v.val.Path()
}
// Proxy function to the underlying cue.Value
func (v *Value) Decode(x interface{}) error {
return v.val.Decode(x)
}
func (v *Value) List() ([]*Value, error) {
l := []*Value{}
it, err := v.val.List()
if err != nil {
return nil, err
}
for it.Next() {
l = append(l, v.Wrap(it.Value()))
}
return l, nil
}
// FIXME: deprecate to simplify
func (v *Value) RangeList(fn func(int, *Value) error) error {
it, err := v.val.List()
if err != nil {
return err
}
i := 0
for it.Next() {
if err := fn(i, v.Wrap(it.Value())); err != nil {
return err
}
i++
}
return nil
}
func (v *Value) RangeStruct(fn func(string, *Value) error) error {
it, err := v.Fields()
if err != nil {
return err
}
for it.Next() {
if err := fn(it.Label(), v.Wrap(it.Value())); err != nil {
return err
}
}
return nil
}
// FIXME: receive string path?
func (v *Value) Merge(x interface{}, path ...string) (*Value, error) {
if xval, ok := x.(*Value); ok {
x = xval.val
}
v.cc.lock()
result := v.Wrap(v.val.Fill(x, path...))
v.cc.unlock()
return result, result.Validate()
}
func (v *Value) MergePath(x interface{}, p cue.Path) (*Value, error) {
// FIXME: array indexes and defs are not supported,
// they will be silently converted to regular fields.
// eg. `foo.#bar[0]` will become `foo["#bar"]["0"]`
return v.Merge(x, cuePathToStrings(p)...)
}
func (v *Value) MergeTarget(x interface{}, target string) (*Value, error) {
return v.MergePath(x, cue.ParsePath(target))
}
// Recursive concreteness check.
func (v *Value) IsConcreteR() error {
return v.val.Validate(cue.Concrete(true))
}
func (v *Value) Walk(before func(*Value) bool, after func(*Value)) {
// FIXME: lock?
var (
llBefore func(cue.Value) bool
llAfter func(cue.Value)
)
if before != nil {
llBefore = func(child cue.Value) bool {
return before(v.Wrap(child))
}
}
if after != nil {
llAfter = func(child cue.Value) {
after(v.Wrap(child))
}
}
v.val.Walk(llBefore, llAfter)
}
// Export concrete values to JSON. ignoring non-concrete values.
// Contrast with cue.Value.MarshalJSON which requires all values
// to be concrete.
func (v *Value) JSON() JSON {
var out JSON
v.val.Walk(
func(v cue.Value) bool {
b, err := v.MarshalJSON()
if err == nil {
newOut, err := out.Set(b, cuePathToStrings(v.Path())...)
if err == nil {
out = newOut
}
return false
}
return true
},
nil,
)
out, _ = out.Get(cuePathToStrings(v.Path())...)
return out
}
// Check that a value is valid. Optionally check that it matches
// all the specified spec definitions..
func (v *Value) Validate() error {
return v.val.Validate()
}
// Return cue source for this value
func (v *Value) Source() ([]byte, error) {
v.cc.rlock()
defer v.cc.runlock()
return cueformat.Node(v.val.Eval().Syntax())
}
// Return cue source for this value, as a Go string
func (v *Value) SourceString() (string, error) {
b, err := v.Source()
return string(b), err
}
func (v *Value) IsEmptyStruct() bool {
if st, err := v.Struct(); err == nil {
if st.Len() == 0 {
return true
}
}
return false
}
func (v *Value) Cue() cue.Value {
return v.val
}