Skip to content

Commit 188b5bc

Browse files
committed
pkg/aflow: support for optional tool args
1 parent f7caedb commit 188b5bc

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pkg/aflow/schema.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"iter"
1010
"maps"
1111
"reflect"
12+
"strings"
1213

1314
"github.com/google/jsonschema-go/jsonschema"
1415
)
@@ -62,6 +63,10 @@ func convertFromMap[T any](m map[string]any, strict, tool bool) (T, error) {
6263
for name, field := range foreachField(&val) {
6364
f, ok := m[name]
6465
if !ok {
66+
fieldType, _ := reflect.TypeFor[T]().FieldByName(name)
67+
if strings.Contains(fieldType.Tag.Get("json"), ",omitempty") {
68+
continue
69+
}
6570
if tool {
6671
return val, BadCallError(fmt.Sprintf("missing argument %q", name))
6772
} else {

pkg/aflow/schema_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ func TestConvertFromMap(t *testing.T) {
7979
testConvertFromMap(t, true, map[string]any{
8080
"I1": 2.0,
8181
}, struct {
82-
I0 int
82+
I0 int `json:"I0"`
8383
}{},
8484
`missing argument "I0"`,
85-
`field "I0" is not present when converting map to struct { I0 int }`)
85+
`field "I0" is not present when converting map to struct { I0 int "json:\"I0\"" }`)
8686

8787
testConvertFromMap(t, true, map[string]any{
8888
"I0": "foo",
@@ -108,6 +108,14 @@ func TestConvertFromMap(t *testing.T) {
108108
}{},
109109
`unused fields when converting map to struct { I0 int }: map[I1:2]`,
110110
`unused fields when converting map to struct { I0 int }: map[I1:2]`)
111+
112+
testConvertFromMap(t, false, map[string]any{
113+
"I1": 2.0,
114+
}, struct {
115+
I0 int `json:",omitempty"`
116+
}{},
117+
``,
118+
``)
111119
}
112120

113121
func testConvertFromMap[T any](t *testing.T, strict bool, input map[string]any, output T, toolErr, nonToolErr string) {

0 commit comments

Comments
 (0)