Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CUE: a data constraint language and shoo-in for...

CUE: a data constraint language and shoo-in for Go – Marcel van Lohuizen

GopherCon Russia

April 13, 2019
Tweet

More Decks by GopherCon Russia

Other Decks in Programming

Transcript

  1. CUE: a data constraint language and shoo-in for Go @mpvl_

    Google, Go team https://cue.googlesource.com/cue https://github.com/cuelang/cue
  2. Image source: Pixabay. View of Street from a Glass Window

    CC images What could be done better?
  3. ESSENTIAL PROPERTIES !7 A & A ≡ A A &

    B ≡ B & A (A & B) & C ≡ A & (B & C) Associative Commutative Idempotent
  4. TYPES ARE VALUES !9 moscow: {
 name: "Moscow"
 pop: 11.92M


    capital: true
 } JSON equivalent in CUE "moscow": {
 "name": "Moscow",
 "pop": 11920000,
 "capital": true
 }
  5. TYPES ARE VALUES !10 municipality: {
 name: string
 pop: int


    capital: bool
 } Go struct equivalent in CUE type Municipality struct { Name string
 Pop int
 Capital bool
 }
  6. TYPES ARE VALUES !11 largeCapital: {
 name: string
 pop: >5M


    capital: true
 } CUE allows anything in between
  7. CUE CONCEPTS Lattice of Values !12 Image source: I, KSmrq,

    Hasse diagram, powerset of {x,y,z} ordered by inclusion. CC BY-SA 3.0
  8. VALUE LATTICE !13 largeCapital: {
 name: string
 pop: >5M
 capital:

    true
 } municipality: {
 name: string
 pop: int
 capital: bool
 } moscow: {
 name: "Moscow"
 pop: 11.92M
 capital: true
 } Value (JSON) Type (Go struct) Mixed (CUE) more specific
  9. VALUE LATTICE !14 number <5 >3 1 4 _|_ _

    string >3 & <5 =~ "^[a-z]{3}$" > "e" "bar" "foo" "foobar" 8 more specific
  10. OTHER HIGHLIGHTS • Path shorthand: a: { b: { c:

    5 }}} == a b c: 5 • References • Sum types/enums a | b | c • Default values number | *1 • Arithmetic 4 + 5 • String interpolation "Hello \(person)" • List and field comprehensions [ x for x in y ] !17
  11. ENTER THE GOPHER !20 Gopher by Renée French • CUE

    is JSON superset + Go's encoding/json = Profit
  12. DEMO TIME !21 Go code Master Pod CUE from Go

    CUE From User Go code cue get go cue cmd CUE scripting YAML Verified CUE kubectl create
  13. Associate constraints using struct field tags type Sum struct {

    A int `cue:"C-B" json:",omitempty"` B int `cue:"C-A" json:",omitempty"` C int `cue:"A+B" json:",omitempty"` } Go struct with cue field tags a := Sum{A: 1, B: 5, C: 10} err := cuego.Complete(&a)
 fmt.Printf(err==nil) // false a = Sum{A: 1, B: 5} err = cuego.Complete(&a)
 fmt.Printf("%#v\n", a) // Sum{A:1, B:5, C:6} Example: cuego.Complete
  14. cuelang.org/cue/go/cuego.Validate type Setup struct { FileName string Min, Max int

    } func init() { cuego.MustAddConstraints(&Setup{}, `{ Filename: =~".json$" // must be a .json file Min: >100 & <=Max Max: <=10_000 }`) } Associate Go type with CUE fmt.Println( cuego.Validate(&Config{"foo.jso", 120, 500})) // "foo.jso" does not match =~".json$" Usage cuego.Validate
  15. TODOS • Protobuf, OpenAPI, ... • Diffing functionality • Testing

    facilities • Auto-derive templates • Extend scripting layer !25