Marcel van Lohuizen | 2b85fc8 | 2019-04-03 22:43:29 +0200 | [diff] [blame] | 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 | |
| 20 | "cuelang.org/go/cuego" |
| 21 | ) |
| 22 | |
| 23 | func ExampleComplete_structTag() { |
| 24 | type Sum struct { |
| 25 | A int `cue:"C-B" json:",omitempty"` |
| 26 | B int `cue:"C-A" json:",omitempty"` |
| 27 | C int `cue:"A+B" json:",omitempty"` |
| 28 | } |
| 29 | |
| 30 | a := Sum{A: 1, B: 5} |
| 31 | err := cuego.Complete(&a) |
| 32 | fmt.Printf("completed: %#v (err: %v)\n", a, err) |
| 33 | |
| 34 | a = Sum{A: 2, C: 8} |
| 35 | err = cuego.Complete(&a) |
| 36 | fmt.Printf("completed: %#v (err: %v)\n", a, err) |
| 37 | |
| 38 | a = Sum{A: 2, B: 3, C: 8} |
| 39 | err = cuego.Complete(&a) |
| 40 | fmt.Println(err) |
| 41 | |
| 42 | //Output: |
| 43 | // completed: cuego_test.Sum{A:1, B:5, C:6} (err: <nil>) |
| 44 | // completed: cuego_test.Sum{A:2, B:6, C:8} (err: <nil>) |
Marcel van Lohuizen | b0678ab | 2019-12-18 10:30:40 +0100 | [diff] [blame] | 45 | // empty disjunction: conflicting values 5 and 2 |
Marcel van Lohuizen | 2b85fc8 | 2019-04-03 22:43:29 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func ExampleConstrain() { |
| 49 | type Config struct { |
| 50 | Filename string |
| 51 | OptFile string `json:",omitempty"` |
| 52 | MaxCount int |
| 53 | MinCount int |
| 54 | |
| 55 | // TODO: show a field with time.Time |
| 56 | } |
| 57 | |
| 58 | err := cuego.Constrain(&Config{}, `{ |
| 59 | jsonFile = =~".json$" |
| 60 | |
| 61 | // Filename must be defined and have a .json extension |
| 62 | Filename: jsonFile |
| 63 | |
| 64 | // OptFile must be undefined or be a file name with a .json extension |
| 65 | OptFile?: jsonFile |
| 66 | |
| 67 | MinCount: >0 & <=MaxCount |
| 68 | MaxCount: <=10_000 |
| 69 | }`) |
| 70 | |
| 71 | fmt.Println("error:", err) |
| 72 | |
| 73 | fmt.Println("validate:", cuego.Validate(&Config{ |
| 74 | Filename: "foo.json", |
| 75 | MaxCount: 1200, |
| 76 | MinCount: 39, |
| 77 | })) |
| 78 | |
| 79 | fmt.Println("validate:", cuego.Validate(&Config{ |
| 80 | Filename: "foo.json", |
| 81 | MaxCount: 12, |
| 82 | MinCount: 39, |
| 83 | })) |
| 84 | |
| 85 | fmt.Println("validate:", cuego.Validate(&Config{ |
| 86 | Filename: "foo.jso", |
| 87 | MaxCount: 120, |
| 88 | MinCount: 39, |
| 89 | })) |
| 90 | |
| 91 | //Output: |
| 92 | // error: <nil> |
| 93 | // validate: <nil> |
Marcel van Lohuizen | c3c1624 | 2020-02-12 17:33:27 +0100 | [diff] [blame] | 94 | // validate: MinCount: invalid value 39 (out of bound <=12) |
| 95 | // validate: Filename: invalid value "foo.jso" (does not match =~".json$") |
Marcel van Lohuizen | 2b85fc8 | 2019-04-03 22:43:29 +0200 | [diff] [blame] | 96 | } |