Skip to content

Commit fd05bf4

Browse files
committed
pkg/uuid: implementation of hermetic UUID functions
These can later be extended with functionality dynamic functions, using the injection of random numbers technique proposed before. This obviates the need to put them in the tools direcotry. Issue cue-lang#192 Change-Id: If82c25fcafe45655b3dd1300501fea181bc4a92f Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9566 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 8823e2a commit fd05bf4

File tree

8 files changed

+413
-2
lines changed

8 files changed

+413
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require (
55
github.com/cockroachdb/apd/v2 v2.0.1
66
github.com/emicklei/proto v1.6.15
77
github.com/google/go-cmp v0.4.0
8+
github.com/google/uuid v1.2.0
89
github.com/kr/pretty v0.1.0
910
github.com/kylelemons/godebug v1.1.0
1011
github.com/lib/pq v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ
4545
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
4646
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
4747
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
48-
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
49-
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
48+
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
49+
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5050
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
5151
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
5252
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=

pkg/register.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ import (
4545
_ "cuelang.org/go/pkg/tool/file"
4646
_ "cuelang.org/go/pkg/tool/http"
4747
_ "cuelang.org/go/pkg/tool/os"
48+
_ "cuelang.org/go/pkg/uuid"
4849
)

pkg/uuid/pkg.go

Lines changed: 159 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/uuid/testdata/uuid.txtar

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
-- in.cue --
2+
import "uuid"
3+
4+
sha1: a: uuid.SHA1(uuid.ns.DNS, "cuelang.org")
5+
6+
md5: a: uuid.MD5(uuid.ns.URL, "https://cuelang.org")
7+
8+
valid: {
9+
a: uuid.Valid
10+
a: "052ef62d-7223-58b6-a551-c1deee46d401"
11+
}
12+
13+
invalid: {
14+
a: uuid.Valid
15+
a: "052EF62D-7223-58B6-A551-C1DEEE46D401"
16+
17+
b: uuid.Valid
18+
b: "052ef62d_7223_58b6_a551_c1deee46d401"
19+
}
20+
21+
parse: a: uuid.Parse("052ef62d722358b6a551c1deee46d401")
22+
23+
fromInt: a: uuid.FromInt(0x052ef62d_7223_58b6_a551_c1deee46d401)
24+
25+
variant: a: uuid.Variant(sha1.a)
26+
version: a: uuid.Version(sha1.a)
27+
urn: a: uuid.URN(sha1.a)
28+
toInt: a: uuid.ToInt(sha1.a)
29+
30+
-- out/uuid --
31+
Errors:
32+
invalid.a: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401":
33+
./in.cue:14:8
34+
invalid.b: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401":
35+
./in.cue:17:8
36+
37+
Result:
38+
sha1: {
39+
a: "052ef62d-7223-58b6-a551-c1deee46d401"
40+
}
41+
md5: {
42+
a: "d891d69e-ae5c-39e0-9ead-164abd207f1f"
43+
}
44+
valid: {
45+
a: "052ef62d-7223-58b6-a551-c1deee46d401"
46+
}
47+
invalid: {
48+
a: _|_ // invalid.a: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): invalid.a: invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401"
49+
b: _|_ // invalid.b: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid.b: invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401"
50+
}
51+
parse: {
52+
a: "052ef62d-7223-58b6-a551-c1deee46d401"
53+
}
54+
fromInt: {
55+
a: "052ef62d-7223-58b6-a551-c1deee46d401"
56+
}
57+
variant: {
58+
a: 1
59+
}
60+
version: {
61+
a: 5
62+
}
63+
urn: {
64+
a: "urn:uuid:052ef62d-7223-58b6-a551-c1deee46d401"
65+
}
66+
toInt: {
67+
a: 93651793875903522077150095950593860979557386807737776869062002310283964632724204171313
68+
}
69+

pkg/uuid/uuid.cue

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2021 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 uuid
16+
17+
// Predefined namespaces
18+
ns: {
19+
DNS: "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
20+
URL: "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
21+
OID: "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
22+
X500: "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
23+
Nil: "00000000-0000-0000-0000-000000000000"
24+
}
25+
26+
// Invalid UUID
27+
variants: Invalid: 0
28+
// The variant specified in RFC4122
29+
variants: RFC4122: 1
30+
// Reserved, NCS backward compatibility.
31+
variants: Reserved: 2
32+
// Reserved, Microsoft Corporation backward compatibility.
33+
variants: Microsoft: 3
34+
// Reserved for future definition.
35+
variants: Future: 4

0 commit comments

Comments
 (0)