cuelang.org/go@v0.10.1/cuego/examples_test.go (about) 1 // Copyright 2019 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 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 //Output: 45 // completed: cuego_test.Sum{A:1, B:5, C:6} (err: <nil>) 46 // completed: cuego_test.Sum{A:2, B:6, C:8} (err: <nil>) 47 // 2 errors in empty disjunction: 48 // conflicting values null and {A:2,B:3,C:8} (mismatched types null and struct) 49 // A: conflicting values 5 and 2 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 // TODO: show a field with time.Time 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 // TODO(errors): fix bound message (should be "does not match") 96 97 //Output: 98 // error: nil 99 // validate: nil 100 // validate: 2 errors in empty disjunction: 101 // conflicting values null and {Filename:"foo.json",MaxCount:12,MinCount:39} (mismatched types null and struct) 102 // MinCount: invalid value 39 (out of bound <=12) 103 // validate: 2 errors in empty disjunction: 104 // conflicting values null and {Filename:"foo.jso",MaxCount:120,MinCount:39} (mismatched types null and struct) 105 // Filename: invalid value "foo.jso" (out of bound =~".json$") 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 }