Files
oikos/vendor/github.com/openai/openai-go/internal/apijson/subfield.go
dtoro febc153b7f
Some checks failed
ci / build-test (push) Has been cancelled
ci / docker-build (push) Has been cancelled
ci / web (push) Has been cancelled
Desktop App / Build Linux (amd64) (push) Has been cancelled
Desktop App / Attach to Release (push) Has been cancelled
fix: add involves edge from task to agent:nomos at creation
Plus sync vendor directory for Docker build compatibility.
2026-08-11 22:03:12 +02:00

68 lines
1.6 KiB
Go

package apijson
import (
"github.com/openai/openai-go/packages/respjson"
"reflect"
)
func getSubField(root reflect.Value, index []int, name string) reflect.Value {
strct := root.FieldByIndex(index[:len(index)-1])
if !strct.IsValid() {
panic("couldn't find encapsulating struct for field " + name)
}
meta := strct.FieldByName("JSON")
if !meta.IsValid() {
return reflect.Value{}
}
field := meta.FieldByName(name)
if !field.IsValid() {
return reflect.Value{}
}
return field
}
func setMetadataSubField(root reflect.Value, index []int, name string, meta Field) {
target := getSubField(root, index, name)
if !target.IsValid() {
return
}
if target.Type() == reflect.TypeOf(meta) {
target.Set(reflect.ValueOf(meta))
} else if respMeta := meta.toRespField(); target.Type() == reflect.TypeOf(respMeta) {
target.Set(reflect.ValueOf(respMeta))
}
}
func setMetadataExtraFields(root reflect.Value, index []int, name string, metaExtras map[string]Field) {
target := getSubField(root, index, name)
if !target.IsValid() {
return
}
if target.Type() == reflect.TypeOf(metaExtras) {
target.Set(reflect.ValueOf(metaExtras))
return
}
newMap := make(map[string]respjson.Field, len(metaExtras))
if target.Type() == reflect.TypeOf(newMap) {
for k, v := range metaExtras {
newMap[k] = v.toRespField()
}
target.Set(reflect.ValueOf(newMap))
}
}
func (f Field) toRespField() respjson.Field {
if f.IsMissing() {
return respjson.Field{}
} else if f.IsNull() {
return respjson.NewField("null")
} else if f.IsInvalid() {
return respjson.NewInvalidField(f.raw)
} else {
return respjson.NewField(f.raw)
}
}