2023-07-02 09:13:51 +00:00
|
|
|
package grammar
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Function struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Parameters map[string]interface{} `json:"parameters"`
|
|
|
|
}
|
|
|
|
type Functions []Function
|
|
|
|
|
2023-07-06 16:05:52 +00:00
|
|
|
func (f Functions) ToJSONStructure() JSONFunctionStructure {
|
|
|
|
js := JSONFunctionStructure{}
|
2023-07-02 09:13:51 +00:00
|
|
|
for _, function := range f {
|
|
|
|
// t := function.Parameters["type"]
|
|
|
|
//tt := t.(string)
|
|
|
|
|
|
|
|
properties := function.Parameters["properties"]
|
2023-07-19 20:56:13 +00:00
|
|
|
defs := function.Parameters["$defs"]
|
2023-07-02 09:13:51 +00:00
|
|
|
dat, _ := json.Marshal(properties)
|
2023-07-19 20:56:13 +00:00
|
|
|
dat2, _ := json.Marshal(defs)
|
2023-07-02 09:13:51 +00:00
|
|
|
prop := map[string]interface{}{}
|
2023-07-19 20:56:13 +00:00
|
|
|
defsD := map[string]interface{}{}
|
|
|
|
|
2023-07-02 09:13:51 +00:00
|
|
|
json.Unmarshal(dat, &prop)
|
2023-07-19 20:56:13 +00:00
|
|
|
json.Unmarshal(dat2, &defsD)
|
|
|
|
if js.Defs == nil {
|
|
|
|
js.Defs = defsD
|
|
|
|
}
|
2023-07-02 09:13:51 +00:00
|
|
|
js.OneOf = append(js.OneOf, Item{
|
|
|
|
Type: "object",
|
|
|
|
Properties: Properties{
|
|
|
|
Function: FunctionName{Const: function.Name},
|
|
|
|
Arguments: Argument{
|
|
|
|
Type: "object",
|
|
|
|
Properties: prop,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return js
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select returns a list of functions containing the function with the given name
|
|
|
|
func (f Functions) Select(name string) Functions {
|
|
|
|
var funcs Functions
|
|
|
|
|
|
|
|
for _, f := range f {
|
|
|
|
if f.Name == name {
|
|
|
|
funcs = []Function{f}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return funcs
|
|
|
|
}
|