summaryrefslogtreecommitdiff
path: root/vendor/github.com/creasty
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-22 17:35:49 -0600
committermo khan <mo@mokhan.ca>2025-07-22 17:35:49 -0600
commit20ef0d92694465ac86b550df139e8366a0a2b4fa (patch)
tree3f14589e1ce6eb9306a3af31c3a1f9e1af5ed637 /vendor/github.com/creasty
parent44e0d272c040cdc53a98b9f1dc58ae7da67752e6 (diff)
feat: connect to spicedb
Diffstat (limited to 'vendor/github.com/creasty')
-rw-r--r--vendor/github.com/creasty/defaults/.gitignore1
-rw-r--r--vendor/github.com/creasty/defaults/LICENSE22
-rw-r--r--vendor/github.com/creasty/defaults/Makefile29
-rw-r--r--vendor/github.com/creasty/defaults/README.md160
-rw-r--r--vendor/github.com/creasty/defaults/defaults.go244
-rw-r--r--vendor/github.com/creasty/defaults/setter.go12
6 files changed, 468 insertions, 0 deletions
diff --git a/vendor/github.com/creasty/defaults/.gitignore b/vendor/github.com/creasty/defaults/.gitignore
new file mode 100644
index 0000000..e43b0f9
--- /dev/null
+++ b/vendor/github.com/creasty/defaults/.gitignore
@@ -0,0 +1 @@
+.DS_Store
diff --git a/vendor/github.com/creasty/defaults/LICENSE b/vendor/github.com/creasty/defaults/LICENSE
new file mode 100644
index 0000000..1483dd2
--- /dev/null
+++ b/vendor/github.com/creasty/defaults/LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2017-present Yuki Iwanaga
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/github.com/creasty/defaults/Makefile b/vendor/github.com/creasty/defaults/Makefile
new file mode 100644
index 0000000..404212a
--- /dev/null
+++ b/vendor/github.com/creasty/defaults/Makefile
@@ -0,0 +1,29 @@
+SHELL := /bin/bash -eu -o pipefail
+
+GO_TEST_FLAGS := -v
+
+PACKAGE_DIRS := $(shell go list ./... 2> /dev/null | grep -v /vendor/)
+SRC_FILES := $(shell find . -name '*.go' -not -path './vendor/*')
+
+
+# Tasks
+#-----------------------------------------------
+.PHONY: lint
+lint:
+ @gofmt -e -d -s $(SRC_FILES) | awk '{ e = 1; print $0 } END { if (e) exit(1) }'
+ @golangci-lint --disable errcheck,unused run
+
+.PHONY: test
+test: lint
+ @go test $(GO_TEST_FLAGS) $(PACKAGE_DIRS)
+
+.PHONY: ci-test
+ci-test: lint
+ @echo > coverage.txt
+ @for d in $(PACKAGE_DIRS); do \
+ go test -coverprofile=profile.out -covermode=atomic -race -v $$d; \
+ if [ -f profile.out ]; then \
+ cat profile.out >> coverage.txt; \
+ rm profile.out; \
+ fi; \
+ done
diff --git a/vendor/github.com/creasty/defaults/README.md b/vendor/github.com/creasty/defaults/README.md
new file mode 100644
index 0000000..e8096b6
--- /dev/null
+++ b/vendor/github.com/creasty/defaults/README.md
@@ -0,0 +1,160 @@
+defaults
+========
+
+[![CircleCI](https://circleci.com/gh/creasty/defaults/tree/master.svg?style=svg)](https://circleci.com/gh/creasty/defaults/tree/master)
+[![codecov](https://codecov.io/gh/creasty/defaults/branch/master/graph/badge.svg)](https://codecov.io/gh/creasty/defaults)
+[![GitHub release](https://img.shields.io/github/release/creasty/defaults.svg)](https://github.com/creasty/defaults/releases)
+[![License](https://img.shields.io/github/license/creasty/defaults.svg)](./LICENSE)
+
+Initialize structs with default values
+
+- Supports almost all kind of types
+ - Scalar types
+ - `int/8/16/32/64`, `uint/8/16/32/64`, `float32/64`
+ - `uintptr`, `bool`, `string`
+ - Complex types
+ - `map`, `slice`, `struct`
+ - Nested types
+ - `map[K1]map[K2]Struct`, `[]map[K1]Struct[]`
+ - Aliased types
+ - `time.Duration`
+ - e.g., `type Enum string`
+ - Pointer types
+ - e.g., `*SampleStruct`, `*int`
+- Recursively initializes fields in a struct
+- Dynamically sets default values by [`defaults.Setter`](./setter.go) interface
+- Preserves non-initial values from being reset with a default value
+
+
+Usage
+-----
+
+```go
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "math/rand"
+
+ "github.com/creasty/defaults"
+)
+
+type Gender string
+
+type Sample struct {
+ Name string `default:"John Smith"`
+ Age int `default:"27"`
+ Gender Gender `default:"m"`
+ Working bool `default:"true"`
+
+ SliceInt []int `default:"[1, 2, 3]"`
+ SlicePtr []*int `default:"[1, 2, 3]"`
+ SliceString []string `default:"[\"a\", \"b\"]"`
+
+ MapNull map[string]int `default:"{}"`
+ Map map[string]int `default:"{\"key1\": 123}"`
+ MapOfStruct map[string]OtherStruct `default:"{\"Key2\": {\"Foo\":123}}"`
+ MapOfPtrStruct map[string]*OtherStruct `default:"{\"Key3\": {\"Foo\":123}}"`
+ MapOfStructWithTag map[string]OtherStruct `default:"{\"Key4\": {\"Foo\":123}}"`
+
+ Struct OtherStruct `default:"{\"Foo\": 123}"`
+ StructPtr *OtherStruct `default:"{\"Foo\": 123}"`
+
+ NoTag OtherStruct // Recurses into a nested struct by default
+ NoOption OtherStruct `default:"-"` // no option
+}
+
+type OtherStruct struct {
+ Hello string `default:"world"` // Tags in a nested struct also work
+ Foo int `default:"-"`
+ Random int `default:"-"`
+}
+
+// SetDefaults implements defaults.Setter interface
+func (s *OtherStruct) SetDefaults() {
+ if defaults.CanUpdate(s.Random) { // Check if it's a zero value (recommended)
+ s.Random = rand.Int() // Set a dynamic value
+ }
+}
+
+func main() {
+ obj := &Sample{}
+ if err := defaults.Set(obj); err != nil {
+ panic(err)
+ }
+
+ out, err := json.MarshalIndent(obj, "", " ")
+ if err != nil {
+ panic(err)
+ }
+ fmt.Println(string(out))
+
+ // Output:
+ // {
+ // "Name": "John Smith",
+ // "Age": 27,
+ // "Gender": "m",
+ // "Working": true,
+ // "SliceInt": [
+ // 1,
+ // 2,
+ // 3
+ // ],
+ // "SlicePtr": [
+ // 1,
+ // 2,
+ // 3
+ // ],
+ // "SliceString": [
+ // "a",
+ // "b"
+ // ],
+ // "MapNull": {},
+ // "Map": {
+ // "key1": 123
+ // },
+ // "MapOfStruct": {
+ // "Key2": {
+ // "Hello": "world",
+ // "Foo": 123,
+ // "Random": 5577006791947779410
+ // }
+ // },
+ // "MapOfPtrStruct": {
+ // "Key3": {
+ // "Hello": "world",
+ // "Foo": 123,
+ // "Random": 8674665223082153551
+ // }
+ // },
+ // "MapOfStructWithTag": {
+ // "Key4": {
+ // "Hello": "world",
+ // "Foo": 123,
+ // "Random": 6129484611666145821
+ // }
+ // },
+ // "Struct": {
+ // "Hello": "world",
+ // "Foo": 123,
+ // "Random": 4037200794235010051
+ // },
+ // "StructPtr": {
+ // "Hello": "world",
+ // "Foo": 123,
+ // "Random": 3916589616287113937
+ // },
+ // "NoTag": {
+ // "Hello": "world",
+ // "Foo": 0,
+ // "Random": 6334824724549167320
+ // },
+ // "NoOption": {
+ // "Hello": "",
+ // "Foo": 0,
+ // "Random": 0
+ // }
+ // }
+}
+```
diff --git a/vendor/github.com/creasty/defaults/defaults.go b/vendor/github.com/creasty/defaults/defaults.go
new file mode 100644
index 0000000..f453928
--- /dev/null
+++ b/vendor/github.com/creasty/defaults/defaults.go
@@ -0,0 +1,244 @@
+package defaults
+
+import (
+ "encoding"
+ "encoding/json"
+ "errors"
+ "reflect"
+ "strconv"
+ "time"
+)
+
+var (
+ errInvalidType = errors.New("not a struct pointer")
+)
+
+const (
+ fieldName = "default"
+)
+
+// Set initializes members in a struct referenced by a pointer.
+// Maps and slices are initialized by `make` and other primitive types are set with default values.
+// `ptr` should be a struct pointer
+func Set(ptr interface{}) error {
+ if reflect.TypeOf(ptr).Kind() != reflect.Ptr {
+ return errInvalidType
+ }
+
+ v := reflect.ValueOf(ptr).Elem()
+ t := v.Type()
+
+ if t.Kind() != reflect.Struct {
+ return errInvalidType
+ }
+
+ for i := 0; i < t.NumField(); i++ {
+ if defaultVal := t.Field(i).Tag.Get(fieldName); defaultVal != "-" {
+ if err := setField(v.Field(i), defaultVal); err != nil {
+ return err
+ }
+ }
+ }
+ callSetter(ptr)
+ return nil
+}
+
+// MustSet function is a wrapper of Set function
+// It will call Set and panic if err not equals nil.
+func MustSet(ptr interface{}) {
+ if err := Set(ptr); err != nil {
+ panic(err)
+ }
+}
+
+func setField(field reflect.Value, defaultVal string) error {
+ if !field.CanSet() {
+ return nil
+ }
+
+ if !shouldInitializeField(field, defaultVal) {
+ return nil
+ }
+
+ isInitial := isInitialValue(field)
+ if isInitial {
+ if unmarshalByInterface(field, defaultVal) {
+ return nil
+ }
+
+ switch field.Kind() {
+ case reflect.Bool:
+ if val, err := strconv.ParseBool(defaultVal); err == nil {
+ field.Set(reflect.ValueOf(val).Convert(field.Type()))
+ }
+ case reflect.Int:
+ if val, err := strconv.ParseInt(defaultVal, 0, strconv.IntSize); err == nil {
+ field.Set(reflect.ValueOf(int(val)).Convert(field.Type()))
+ }
+ case reflect.Int8:
+ if val, err := strconv.ParseInt(defaultVal, 0, 8); err == nil {
+ field.Set(reflect.ValueOf(int8(val)).Convert(field.Type()))
+ }
+ case reflect.Int16:
+ if val, err := strconv.ParseInt(defaultVal, 0, 16); err == nil {
+ field.Set(reflect.ValueOf(int16(val)).Convert(field.Type()))
+ }
+ case reflect.Int32:
+ if val, err := strconv.ParseInt(defaultVal, 0, 32); err == nil {
+ field.Set(reflect.ValueOf(int32(val)).Convert(field.Type()))
+ }
+ case reflect.Int64:
+ if val, err := time.ParseDuration(defaultVal); err == nil {
+ field.Set(reflect.ValueOf(val).Convert(field.Type()))
+ } else if val, err := strconv.ParseInt(defaultVal, 0, 64); err == nil {
+ field.Set(reflect.ValueOf(val).Convert(field.Type()))
+ }
+ case reflect.Uint:
+ if val, err := strconv.ParseUint(defaultVal, 0, strconv.IntSize); err == nil {
+ field.Set(reflect.ValueOf(uint(val)).Convert(field.Type()))
+ }
+ case reflect.Uint8:
+ if val, err := strconv.ParseUint(defaultVal, 0, 8); err == nil {
+ field.Set(reflect.ValueOf(uint8(val)).Convert(field.Type()))
+ }
+ case reflect.Uint16:
+ if val, err := strconv.ParseUint(defaultVal, 0, 16); err == nil {
+ field.Set(reflect.ValueOf(uint16(val)).Convert(field.Type()))
+ }
+ case reflect.Uint32:
+ if val, err := strconv.ParseUint(defaultVal, 0, 32); err == nil {
+ field.Set(reflect.ValueOf(uint32(val)).Convert(field.Type()))
+ }
+ case reflect.Uint64:
+ if val, err := strconv.ParseUint(defaultVal, 0, 64); err == nil {
+ field.Set(reflect.ValueOf(val).Convert(field.Type()))
+ }
+ case reflect.Uintptr:
+ if val, err := strconv.ParseUint(defaultVal, 0, strconv.IntSize); err == nil {
+ field.Set(reflect.ValueOf(uintptr(val)).Convert(field.Type()))
+ }
+ case reflect.Float32:
+ if val, err := strconv.ParseFloat(defaultVal, 32); err == nil {
+ field.Set(reflect.ValueOf(float32(val)).Convert(field.Type()))
+ }
+ case reflect.Float64:
+ if val, err := strconv.ParseFloat(defaultVal, 64); err == nil {
+ field.Set(reflect.ValueOf(val).Convert(field.Type()))
+ }
+ case reflect.String:
+ field.Set(reflect.ValueOf(defaultVal).Convert(field.Type()))
+
+ case reflect.Slice:
+ ref := reflect.New(field.Type())
+ ref.Elem().Set(reflect.MakeSlice(field.Type(), 0, 0))
+ if defaultVal != "" && defaultVal != "[]" {
+ if err := json.Unmarshal([]byte(defaultVal), ref.Interface()); err != nil {
+ return err
+ }
+ }
+ field.Set(ref.Elem().Convert(field.Type()))
+ case reflect.Map:
+ ref := reflect.New(field.Type())
+ ref.Elem().Set(reflect.MakeMap(field.Type()))
+ if defaultVal != "" && defaultVal != "{}" {
+ if err := json.Unmarshal([]byte(defaultVal), ref.Interface()); err != nil {
+ return err
+ }
+ }
+ field.Set(ref.Elem().Convert(field.Type()))
+ case reflect.Struct:
+ if defaultVal != "" && defaultVal != "{}" {
+ if err := json.Unmarshal([]byte(defaultVal), field.Addr().Interface()); err != nil {
+ return err
+ }
+ }
+ case reflect.Ptr:
+ field.Set(reflect.New(field.Type().Elem()))
+ }
+ }
+
+ switch field.Kind() {
+ case reflect.Ptr:
+ if isInitial || field.Elem().Kind() == reflect.Struct {
+ setField(field.Elem(), defaultVal)
+ callSetter(field.Interface())
+ }
+ case reflect.Struct:
+ if err := Set(field.Addr().Interface()); err != nil {
+ return err
+ }
+ case reflect.Slice:
+ for j := 0; j < field.Len(); j++ {
+ if err := setField(field.Index(j), ""); err != nil {
+ return err
+ }
+ }
+ case reflect.Map:
+ for _, e := range field.MapKeys() {
+ var v = field.MapIndex(e)
+
+ switch v.Kind() {
+ case reflect.Ptr:
+ switch v.Elem().Kind() {
+ case reflect.Struct, reflect.Slice, reflect.Map:
+ if err := setField(v.Elem(), ""); err != nil {
+ return err
+ }
+ }
+ case reflect.Struct, reflect.Slice, reflect.Map:
+ ref := reflect.New(v.Type())
+ ref.Elem().Set(v)
+ if err := setField(ref.Elem(), ""); err != nil {
+ return err
+ }
+ field.SetMapIndex(e, ref.Elem().Convert(v.Type()))
+ }
+ }
+ }
+
+ return nil
+}
+
+func unmarshalByInterface(field reflect.Value, defaultVal string) bool {
+ asText, ok := field.Addr().Interface().(encoding.TextUnmarshaler)
+ if ok && defaultVal != "" {
+ // if field implements encode.TextUnmarshaler, try to use it before decode by kind
+ if err := asText.UnmarshalText([]byte(defaultVal)); err == nil {
+ return true
+ }
+ }
+ asJSON, ok := field.Addr().Interface().(json.Unmarshaler)
+ if ok && defaultVal != "" && defaultVal != "{}" && defaultVal != "[]" {
+ // if field implements json.Unmarshaler, try to use it before decode by kind
+ if err := asJSON.UnmarshalJSON([]byte(defaultVal)); err == nil {
+ return true
+ }
+ }
+ return false
+}
+
+func isInitialValue(field reflect.Value) bool {
+ return reflect.DeepEqual(reflect.Zero(field.Type()).Interface(), field.Interface())
+}
+
+func shouldInitializeField(field reflect.Value, tag string) bool {
+ switch field.Kind() {
+ case reflect.Struct:
+ return true
+ case reflect.Ptr:
+ if !field.IsNil() && field.Elem().Kind() == reflect.Struct {
+ return true
+ }
+ case reflect.Slice:
+ return field.Len() > 0 || tag != ""
+ case reflect.Map:
+ return field.Len() > 0 || tag != ""
+ }
+
+ return tag != ""
+}
+
+// CanUpdate returns true when the given value is an initial value of its type
+func CanUpdate(v interface{}) bool {
+ return isInitialValue(reflect.ValueOf(v))
+}
diff --git a/vendor/github.com/creasty/defaults/setter.go b/vendor/github.com/creasty/defaults/setter.go
new file mode 100644
index 0000000..1f64aa6
--- /dev/null
+++ b/vendor/github.com/creasty/defaults/setter.go
@@ -0,0 +1,12 @@
+package defaults
+
+// Setter is an interface for setting default values
+type Setter interface {
+ SetDefaults()
+}
+
+func callSetter(v interface{}) {
+ if ds, ok := v.(Setter); ok {
+ ds.SetDefaults()
+ }
+}