...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package cuego_test
16
17 import (
18 "fmt"
19 "strings"
20
21 "cuelang.org/go/cue/errors"
22 "cuelang.org/go/cuego"
23 )
24
25 func ExampleComplete_structTag() {
26 type Sum struct {
27 A int `cue:"C-B" json:",omitempty"`
28 B int `cue:"C-A" json:",omitempty"`
29 C int `cue:"A+B" json:",omitempty"`
30 }
31
32 a := Sum{A: 1, B: 5}
33 err := cuego.Complete(&a)
34 fmt.Printf("completed: %#v (err: %v)\n", a, err)
35
36 a = Sum{A: 2, C: 8}
37 err = cuego.Complete(&a)
38 fmt.Printf("completed: %#v (err: %v)\n", a, err)
39
40 a = Sum{A: 2, B: 3, C: 8}
41 err = cuego.Complete(&a)
42 fmt.Println(errMsg(err))
43
44
45
46
47
48
49
50 }
51
52 func ExampleConstrain() {
53 type Config struct {
54 Filename string
55 OptFile string `json:",omitempty"`
56 MaxCount int
57 MinCount int
58
59
60 }
61
62 err := cuego.Constrain(&Config{}, `{
63 let jsonFile = =~".json$"
64
65 // Filename must be defined and have a .json extension
66 Filename: jsonFile
67
68 // OptFile must be undefined or be a file name with a .json extension
69 OptFile?: jsonFile
70
71 MinCount: >0 & <=MaxCount
72 MaxCount: <=10_000
73 }`)
74
75 fmt.Println("error:", errMsg(err))
76
77 fmt.Println("validate:", errMsg(cuego.Validate(&Config{
78 Filename: "foo.json",
79 MaxCount: 1200,
80 MinCount: 39,
81 })))
82
83 fmt.Println("validate:", errMsg(cuego.Validate(&Config{
84 Filename: "foo.json",
85 MaxCount: 12,
86 MinCount: 39,
87 })))
88
89 fmt.Println("validate:", errMsg(cuego.Validate(&Config{
90 Filename: "foo.jso",
91 MaxCount: 120,
92 MinCount: 39,
93 })))
94
95
96
97
98
99
100
101
102
103
104
105
106 }
107
108 func errMsg(err error) string {
109 a := []string{}
110 for _, err := range errors.Errors(err) {
111 a = append(a, err.Error())
112 }
113 s := strings.Join(a, "\n")
114 if s == "" {
115 return "nil"
116 }
117 return s
118 }
119
View as plain text