Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
pkg/uuid: implementation of hermetic UUID functions
Browse files Browse the repository at this point in the history
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 #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]>
  • Loading branch information
mpvl committed Apr 30, 2021
1 parent 8823e2a commit fd05bf4
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/cockroachdb/apd/v2 v2.0.1
github.com/emicklei/proto v1.6.15
github.com/google/go-cmp v0.4.0
github.com/google/uuid v1.2.0
github.com/kr/pretty v0.1.0
github.com/kylelemons/godebug v1.1.0
github.com/lib/pq v1.0.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down
1 change: 1 addition & 0 deletions pkg/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ import (
_ "cuelang.org/go/pkg/tool/file"
_ "cuelang.org/go/pkg/tool/http"
_ "cuelang.org/go/pkg/tool/os"
_ "cuelang.org/go/pkg/uuid"
)
159 changes: 159 additions & 0 deletions pkg/uuid/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions pkg/uuid/testdata/uuid.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
-- in.cue --
import "uuid"

sha1: a: uuid.SHA1(uuid.ns.DNS, "cuelang.org")

md5: a: uuid.MD5(uuid.ns.URL, "https://cuelang.org")

valid: {
a: uuid.Valid
a: "052ef62d-7223-58b6-a551-c1deee46d401"
}

invalid: {
a: uuid.Valid
a: "052EF62D-7223-58B6-A551-C1DEEE46D401"

b: uuid.Valid
b: "052ef62d_7223_58b6_a551_c1deee46d401"
}

parse: a: uuid.Parse("052ef62d722358b6a551c1deee46d401")

fromInt: a: uuid.FromInt(0x052ef62d_7223_58b6_a551_c1deee46d401)

variant: a: uuid.Variant(sha1.a)
version: a: uuid.Version(sha1.a)
urn: a: uuid.URN(sha1.a)
toInt: a: uuid.ToInt(sha1.a)

-- out/uuid --
Errors:
invalid.a: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401":
./in.cue:14:8
invalid.b: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401":
./in.cue:17:8

Result:
sha1: {
a: "052ef62d-7223-58b6-a551-c1deee46d401"
}
md5: {
a: "d891d69e-ae5c-39e0-9ead-164abd207f1f"
}
valid: {
a: "052ef62d-7223-58b6-a551-c1deee46d401"
}
invalid: {
a: _|_ // invalid.a: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): invalid.a: invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401"
b: _|_ // invalid.b: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid.b: invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401"
}
parse: {
a: "052ef62d-7223-58b6-a551-c1deee46d401"
}
fromInt: {
a: "052ef62d-7223-58b6-a551-c1deee46d401"
}
variant: {
a: 1
}
version: {
a: 5
}
urn: {
a: "urn:uuid:052ef62d-7223-58b6-a551-c1deee46d401"
}
toInt: {
a: 93651793875903522077150095950593860979557386807737776869062002310283964632724204171313
}

35 changes: 35 additions & 0 deletions pkg/uuid/uuid.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package uuid

// Predefined namespaces
ns: {
DNS: "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
URL: "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
OID: "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
X500: "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
Nil: "00000000-0000-0000-0000-000000000000"
}

// Invalid UUID
variants: Invalid: 0
// The variant specified in RFC4122
variants: RFC4122: 1
// Reserved, NCS backward compatibility.
variants: Reserved: 2
// Reserved, Microsoft Corporation backward compatibility.
variants: Microsoft: 3
// Reserved for future definition.
variants: Future: 4
Loading

0 comments on commit fd05bf4

Please sign in to comment.