summaryrefslogtreecommitdiff
path: root/vendor/github.com/lann/builder/registry.go
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/lann/builder/registry.go
parent44e0d272c040cdc53a98b9f1dc58ae7da67752e6 (diff)
feat: connect to spicedb
Diffstat (limited to 'vendor/github.com/lann/builder/registry.go')
-rw-r--r--vendor/github.com/lann/builder/registry.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/vendor/github.com/lann/builder/registry.go b/vendor/github.com/lann/builder/registry.go
new file mode 100644
index 0000000..6128454
--- /dev/null
+++ b/vendor/github.com/lann/builder/registry.go
@@ -0,0 +1,59 @@
+package builder
+
+import (
+ "reflect"
+ "sync"
+)
+
+var (
+ registry = make(map[reflect.Type]reflect.Type)
+ registryMux sync.RWMutex
+)
+
+// RegisterType maps the given builderType to a structType.
+// This mapping affects the type of slices returned by Get and is required for
+// GetStruct to work.
+//
+// Returns a Value containing an empty instance of the registered builderType.
+//
+// RegisterType will panic if builderType's underlying type is not Builder or
+// if structType's Kind is not Struct.
+func RegisterType(builderType reflect.Type, structType reflect.Type) *reflect.Value {
+ registryMux.Lock()
+ defer registryMux.Unlock()
+ structType.NumField() // Panic if structType is not a struct
+ registry[builderType] = structType
+ emptyValue := emptyBuilderValue.Convert(builderType)
+ return &emptyValue
+}
+
+// Register wraps RegisterType, taking instances instead of Types.
+//
+// Returns an empty instance of the registered builder type which can be used
+// as the initial value for builder expressions. See example.
+func Register(builderProto, structProto interface{}) interface{} {
+ empty := RegisterType(
+ reflect.TypeOf(builderProto),
+ reflect.TypeOf(structProto),
+ ).Interface()
+ return empty
+}
+
+func getBuilderStructType(builderType reflect.Type) *reflect.Type {
+ registryMux.RLock()
+ defer registryMux.RUnlock()
+ structType, ok := registry[builderType]
+ if !ok {
+ return nil
+ }
+ return &structType
+}
+
+func newBuilderStruct(builderType reflect.Type) *reflect.Value {
+ structType := getBuilderStructType(builderType)
+ if structType == nil {
+ return nil
+ }
+ newStruct := reflect.New(*structType).Elem()
+ return &newStruct
+}