CUE Specification – Numeric Literals

日本語版

https://cuelang.org/docs/references/spec/#numeric-literals

First, I updated CUE because new version was released.

$ go install cuelang.org/go/cmd/cue@latest

$ cue version
cue version v0.5.0

go version go1.18.9
       -compiler gc
     CGO_ENABLED 1
          GOARCH arm64
            GOOS linux

I tried it all roughly.

$ cat numeric.cue
dec: 123
dec_long: 123_456_789
m: 1K
mi: 1Ki
bin: 0b10000
bin_long: 0b100000000_00000000
oct: 0o700
oct_long: 0o700_000_000
hex: 0xff
hex_long: 0xdead_beef
f: 0.1
fn: .2
e: 1e-1
e2: 1.2e-1

$ cue eval numeric.cue
dec:      123
dec_long: 123456789
m:        1000
mi:       1024
bin:      16
bin_long: 65536
oct:      448
oct_long: 117440512
hex:      255
hex_long: 3735928559
f:        0.1
fn:       0.2
e:        0.1
e2:       0.12

It seemed floating point before multiplier with ‘i’ such as Mi is not able to be used, the specification seemed possible. It may be a chance to read the source code.

First touch CUE lang

日本語版

When I though what format shall I use as a program configuration file, I got to know CUE lang.

I could use comments, conditional branch and loop processing, It seems good. And look like also can call event Go lang library!? It may be too many functions for just use as a configuration file…

For now, I touched it.

Install.

$ go install cuelang.org/go/cmd/cue@latest
$ cue version
cue version v0.4.3 linux/arm64

Used such as the following file,

$ cat persons.cue
#Person: {
        name: string
        level: int & >=1 & <=10
        role: "Administrator" | *"Developer"  // Developer is the default
}

alicee: #Person
alicee: {name: "Alice", level: 6 }

bobsmith: #Person
bobsmith: {name: "Bob", level: 5 }

carryz: #Person
bobsmith: {name: "Carry", level: 5 }

Administrators: [ "alicee" ]

for person in Administrators {
        "\(person)": {
                role: "Administrator"
        }
}

the evaluation result was:

$ cue eval persons.cue
#Person: {
    name:  string
    level: uint & >=1 & <=10
    role:  "Developer"
}
alicee: {
    name:  "Alice"
    level: 6
    role:  "Administrator"
}
bobsmith: {
    name:  "Bob"
    level: 5
    role:  "Developer"
}
carryz: {
    name:  "Carry"
    level: 5
    role:  "Developer"
}
Administrators: ["alicee"]