Home

cuesonnet @a190cdc29867c0c5a6cf7cf9f1895e7fbb58845c - refs - log -
-
https://git.jolheiser.com/cuesonnet.git
CUE + Jsonnet
cuesonnet / cuesonnet.go
- raw -
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cuesonnet

import (
	"fmt"
	"io"
	"strings"

	"cuelang.org/go/cue"
	"cuelang.org/go/cue/cuecontext"
	"cuelang.org/go/cue/errors"
	"github.com/google/go-jsonnet"
)

// Schema is a CUE schema
type Schema string

// Decode validates jsonnet r against the CUE schema and unmarshals into v
func (sc Schema) Decode(r io.Reader, v any) error {
	data, err := io.ReadAll(r)
	if err != nil {
		return fmt.Errorf("could not read data: %w", err)
	}

	ctx := cuecontext.New()

	vm := jsonnet.MakeVM()
	jsonData, err := vm.EvaluateAnonymousSnippet("config.jsonnet", string(data))
	if err != nil {
		return fmt.Errorf("could not evaluate jsonnet: %w", err)
	}

	schema := ctx.CompileString(fmt.Sprintf(`
			#_Schema: {
				%s
			}
			data: #_Schema
		`, sc))
	if schema.Err() != nil {
		return fmt.Errorf("invalid schema: %w", schema.Err())
	}

	jsonCUE := ctx.CompileString(fmt.Sprintf(`
			data: %s
		`, jsonData))
	if jsonCUE.Err() != nil {
		return fmt.Errorf("invalid JSON: %w", jsonCUE.Err())
	}

	opts := []cue.Option{
		cue.Concrete(true),
		cue.Final(),
	}
	unified := schema.Unify(jsonCUE)
	if err := unified.Validate(opts...); err != nil {
		var errs []string
		for _, e := range errors.Errors(err) {
			errs = append(errs, strings.TrimPrefix(e.Error(), "data."))
		}
		return fmt.Errorf("config failed validation with %d errors:\n%s", len(errs), strings.Join(errs, "\n"))
	}

	resolved := unified.LookupPath(cue.ParsePath("data"))
	if err := resolved.Decode(&v); err != nil {
		return fmt.Errorf("could not decode unified config: %w", err)
	}

	return nil
}