From 93d154a099adcf1eccfacd1bb3514d418a57abf9 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 27 Nov 2017 11:35:26 +0000 Subject: [PATCH] Add sql functions for generating GUIDs --- sql/funcs.go | 2 ++ util/fake/guid.go | 27 +++++++++++++++++++++++++++ util/fncs/fnc.go | 4 ++++ util/fncs/rand.go | 4 ++++ 4 files changed, 37 insertions(+) create mode 100644 util/fake/guid.go diff --git a/sql/funcs.go b/sql/funcs.go index 0c13af5e..57d3b506 100644 --- a/sql/funcs.go +++ b/sql/funcs.go @@ -251,8 +251,10 @@ var funcs = map[string]map[int]interface{}{ // Random implementation "rand": {0: nil}, + "guid": {0: nil}, "uuid": {0: nil}, "rand.bool": {0: nil}, + "rand.guid": {0: nil}, "rand.uuid": {0: nil}, "rand.enum": {-1: nil}, "rand.time": {0: nil, 2: nil}, diff --git a/util/fake/guid.go b/util/fake/guid.go new file mode 100644 index 00000000..69e03bfd --- /dev/null +++ b/util/fake/guid.go @@ -0,0 +1,27 @@ +// Copyright © 2016 Abcum Ltd +// +// 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 fake + +import ( + "github.com/abcum/surreal/util/guid" +) + +func Guid() string { + return guid.New().String() +} + +func (f *Faker) Guid() string { + return guid.New().String() +} diff --git a/util/fncs/fnc.go b/util/fncs/fnc.go index 95d1d162..5be96409 100644 --- a/util/fncs/fnc.go +++ b/util/fncs/fnc.go @@ -252,10 +252,14 @@ func Run(ctx context.Context, name string, args ...interface{}) (interface{}, er // Rand implementation case "rand": return rand(ctx, args...) + case "guid": + return randGuid(ctx, args...) case "uuid": return randUuid(ctx, args...) case "rand.bool": return randBool(ctx, args...) + case "rand.guid": + return randGuid(ctx, args...) case "rand.uuid": return randUuid(ctx, args...) case "rand.enum": diff --git a/util/fncs/rand.go b/util/fncs/rand.go index 4973f3ac..b142139a 100644 --- a/util/fncs/rand.go +++ b/util/fncs/rand.go @@ -29,6 +29,10 @@ func randBool(ctx context.Context, args ...interface{}) (bool, error) { return fake.Bool(), nil } +func randGuid(ctx context.Context, args ...interface{}) (string, error) { + return fake.Guid(), nil +} + func randUuid(ctx context.Context, args ...interface{}) (string, error) { return fake.Uuid(), nil }