100 lines
2.6 KiB
Go
100 lines
2.6 KiB
Go
|
|
package engine
|
||
|
|
|
||
|
|
import "reflect"
|
||
|
|
|
||
|
|
// ResultValue navigates the current Go value without projecting or copying it.
|
||
|
|
func ResultValue(root any, path Path, secrets SecretBinding) (any, bool) {
|
||
|
|
if !path.valid() {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
current := reflect.ValueOf(root)
|
||
|
|
if len(path.segments) == 0 {
|
||
|
|
return root, true
|
||
|
|
}
|
||
|
|
builder := schemaBuilder{cache: make(map[reflect.Type]*typeDescriptor), building: make(map[reflect.Type]bool), secrets: secrets, lookup: true}
|
||
|
|
for _, segment := range path.segments {
|
||
|
|
seen := make(map[valueVisit]bool)
|
||
|
|
for current.IsValid() && (current.Kind() == reflect.Pointer || current.Kind() == reflect.Interface) {
|
||
|
|
if current.IsNil() {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
if current.Kind() == reflect.Pointer {
|
||
|
|
visit := valueVisit{typeOf: current.Type(), kind: current.Kind(), ptr: current.Pointer()}
|
||
|
|
if seen[visit] {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
seen[visit] = true
|
||
|
|
}
|
||
|
|
current = current.Elem()
|
||
|
|
}
|
||
|
|
if !current.IsValid() {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
descriptor, issue := builder.describe(current.Type(), Path{})
|
||
|
|
if issue != nil {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
switch descriptor.kind {
|
||
|
|
case descriptorStruct:
|
||
|
|
if segment.kind == pathSegmentIndex {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
if position, ok := descriptor.fieldByName[segment.key]; ok {
|
||
|
|
current = resultField(current, descriptor.fields[position].index)
|
||
|
|
} else if descriptor.inlineMap != nil {
|
||
|
|
current = resultMapEntry(resultField(current, descriptor.inlineMap.index), segment.key)
|
||
|
|
} else {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
case descriptorMap:
|
||
|
|
if segment.kind == pathSegmentIndex {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
current = resultMapEntry(current, segment.key)
|
||
|
|
case descriptorSlice, descriptorArray:
|
||
|
|
index, ok := schemaIndex(segment)
|
||
|
|
if !ok || index >= current.Len() {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
current = current.Index(index)
|
||
|
|
default:
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
if !current.IsValid() {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if !current.CanInterface() {
|
||
|
|
return nil, false
|
||
|
|
}
|
||
|
|
return current.Interface(), true
|
||
|
|
}
|
||
|
|
|
||
|
|
func resultField(root reflect.Value, index []int) reflect.Value {
|
||
|
|
current := root
|
||
|
|
for position, field := range index {
|
||
|
|
if current.Kind() != reflect.Struct {
|
||
|
|
return reflect.Value{}
|
||
|
|
}
|
||
|
|
current = current.Field(field)
|
||
|
|
if position < len(index)-1 {
|
||
|
|
for current.Kind() == reflect.Pointer {
|
||
|
|
if current.IsNil() {
|
||
|
|
return reflect.Value{}
|
||
|
|
}
|
||
|
|
current = current.Elem()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return current
|
||
|
|
}
|
||
|
|
|
||
|
|
func resultMapEntry(value reflect.Value, name string) reflect.Value {
|
||
|
|
if !value.IsValid() || value.Kind() != reflect.Map {
|
||
|
|
return reflect.Value{}
|
||
|
|
}
|
||
|
|
key := reflect.New(value.Type().Key()).Elem()
|
||
|
|
key.SetString(name)
|
||
|
|
return value.MapIndex(key)
|
||
|
|
}
|