initial commit of untested function stuff
This commit is contained in:
47
schema/enum.go
Normal file
47
schema/enum.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
|
||||
"golang.org/x/exp/slices"
|
||||
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
)
|
||||
|
||||
type enum struct {
|
||||
basic
|
||||
|
||||
values []string
|
||||
}
|
||||
|
||||
func (e enum) SchemaType() jsonschema.DataType {
|
||||
return jsonschema.String
|
||||
}
|
||||
|
||||
func (e enum) Definition() jsonschema.Definition {
|
||||
def := e.basic.Definition()
|
||||
def.Enum = e.values
|
||||
return def
|
||||
}
|
||||
|
||||
func (e enum) FromAny(val any) (reflect.Value, error) {
|
||||
v := reflect.ValueOf(val)
|
||||
if v.Kind() != reflect.String {
|
||||
return reflect.Value{}, errors.New("expected string, got " + v.Kind().String())
|
||||
}
|
||||
|
||||
s := v.String()
|
||||
if !slices.Contains(e.values, s) {
|
||||
return reflect.Value{}, errors.New("value " + s + " not in enum")
|
||||
}
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (e enum) SetValueOnField(obj reflect.Value, val reflect.Value) {
|
||||
if !e.required {
|
||||
val = val.Addr()
|
||||
}
|
||||
obj.Field(e.index).Set(val)
|
||||
}
|
Reference in New Issue
Block a user