1
0
bind/lib.go
2025-08-28 05:04:06 +02:00

76 lines
1.7 KiB
Go

// provides more flexible and permissive ways of setting values than reflect.Value.Set
// circular type structures not supported
// it handles scalar fields
// primary use cases by design are: command line options, environment variables, ini file fields, URL query parameters, HTTP form values
package bind
type Field struct {
name string
path []string
isBool bool
list bool
free bool
value any
}
// the receiver must be addressable
func BindScalar(receiver any, value ...any) bool {
return bindScalarReflect(receiver, value)
}
func BindScalarCreate[T any](value ...any) (T, bool) {
return bindScalarCreateReflect[T](value)
}
func FieldValue(path []string, value any) Field {
return Field{path: path, value: value}
}
func FieldValueByName(name string, value any) Field {
return Field{name: name, value: value}
}
func (f Field) Path() []string {
p := make([]string, len(f.path))
copy(p, f.path)
return p
}
func (f Field) Name() string { return f.name }
func (f Field) List() bool { return f.list }
func (f Field) Bool() bool { return f.isBool }
func (f Field) Free() bool { return f.free }
func (f Field) Value() any { return f.value }
func Fields[T any]() []Field {
return fieldsReflect[T]()
}
func FieldValues(structure any) []Field {
return fieldValuesReflect(structure)
}
func BindFields(structure any, values []Field) []Field {
return nil
}
func BindFieldsCreate[T any](values []Field) (any, []Field) {
return nil, nil
}
func AcceptsScalar[T any]() bool {
return acceptsScalarReflect[T]()
}
func AcceptsFields[T any]() bool {
return acceptsFieldsReflect[T]()
}
func AcceptsList[T any]() bool {
return acceptsListReflect[T]()
}
func Bindable[T any]() bool {
return bindableReflect[T]()
}