name: inverse class: title,inverse <p>Hello: "CUE" </p> ## anarcher@gmail.com ## 2020-09-01 --- class: pic  --- class: inverse # CUE - Unifies data and schema - Command-line tool - Logic engine - Rich Go APIs and integration - https://cuelang.org --- # Values in CUE - CUE is a superset of JSON. - https://cuelang.org/docs/tutorials/tour/intro/json/ ```cue seoul: { name: "Seoul" pop: 9.7M capital: true } ``` ```json "seoul" : { "name" : "Seoul" "pop" : 9700000 "capital": true } ``` --- # Types are values - https://cuelang.org/docs/tutorials/tour/intro/cue/ ```cue // cue municipality: { name: string pop: int capital: bool } ``` ```go // go type municipality struct { Name string Pop int Capital bool } ``` --- # Types & Constraints - https://cuelang.org/docs/tutorials/tour/intro/constraints/ ```cue largeCapital: { name: string pop: >5M capital: true } ``` ```cue schema: { name: string age: int human: true // always true } viola: schema viola: { name: "Viola" age: 38 } ``` --- # Definitions - https://cuelang.org/docs/tutorials/tour/intro/schema/ - starts with a `#` or `_#` - use for validation, not output ```cue #Conn: { address: string port: int protocol: string // ... // uncomment this to allow any field } lossy: #Conn & { address: "1.2.3.4" port: 8888 protocol: "udp" // foo: 2 // uncomment this to get an error } ``` --- # Modules, Packages, and Instances - https://cuelang.org/docs/concepts/packages/ ```sh cue.mod |-- module.cue // The module file |-- pkg // copies of external packages |-- gen // files generated from external sources |-- usr // user-defined constraints ``` ```cue root // must contain: |-- cue.mod | |-- module.cue // module: "example.com" |-- schemas | |-- trains | | |-- track.cue // package track ... |-- data.cue // import "example.com/schemas/trains:track" <module identifier>/<relative position of package within module>:<package name> ``` --- class: inverse # Modules, Packages, and Instances - https://cuelang.org/docs/concepts/packages/ Within a module, all .cue files with the same package name are part of the same package. Instance of a package: - only the files belonging to that package in that directory and its ancestor directories Example: - module root: `schema` - medial directories: `policy` - leaf directories: `data` --- class: inverse # Default value - https://cuelang.org/docs/tutorials/tour/types/defaults/ ```cue // defaults.cue // any positive number, 1 is the default replicas: uint | *1 // the default value is ambiguous protocol: *"tcp" | "udp" protocol: *"udp" | "tcp" ``` ```cue $ cue eval defaults.cue replicas: 1 protocol: "tcp" | "udp" | *_|_ ``` --- class: inverse ```cue // Conditional Fields price: number // Require a justification if price is too high if price > 100 { justification: string } price: 200 // List Comprehensions [ for x in #items if x rem 2 == 0 { x*x } ] #items: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] // Field Comprehensions import "strings" #a: [ "Barcelona", "Shanghai", "Munich" ] for k, v in #a { "\( strings.ToLower(v) )": { pos: k + 1 name: v nameLen: len(v) } } ``` --- class: inverse # Builtin packages - CUE has a collection of builtin packages that are compiled into the `cue` binary. - https://pkg.go.dev/cuelang.org/go/pkg?tab=doc - `tool/exec.Run`: https://pkg.go.dev/cuelang.org/go@v0.2.2/pkg/tool/exec?tab=doc - Not yet: user defined native packages... # `cue` commands https://github.com/cuelang/cue/blob/master/doc/cmd/cue.md `cmd` executes user-defined named commands for each of the listed instances. - Commands are defined in cue files ending with `_tool.cue` - Command definition: https://github.com/cuelang/cue/blob/master/doc/cmd/cue.md#command-definition --- class: inverse # `cue` commands <pre><code class="remark-code" style="font-size: 80%;">// hello.cue package foo city: "Seoul" who: *"World" | string @tag(who) </code></pre> <pre><code class="remark-code" style="font-size: 80%;">// hello_tool.cue package foo import "tool/exec" command: hello: { print: exec.Run & { cmd: "echo Hello \(who)! Welcome to \(city)." } } </code></pre> <pre><code class="remark-code" style="font-size: 80%;">// hello_tool.cue $ cue --inject who=Jan hello Hello Jan! Welcome to Seoul. $ cue hello Hello World! Welcome to Seoul. </code></pre> --- class: pic .interstitial[] --- name: inverse class: title, inverse Thank you