From 6e304df8df70bfaecd0ff91478acda0e9177f030 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 20 Jan 2026 20:20:12 +0100 Subject: pkg/aflow: support for optional tool args --- pkg/aflow/schema.go | 5 +++++ pkg/aflow/schema_test.go | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) (limited to 'pkg') diff --git a/pkg/aflow/schema.go b/pkg/aflow/schema.go index a563a4bdf..855bfc303 100644 --- a/pkg/aflow/schema.go +++ b/pkg/aflow/schema.go @@ -9,6 +9,7 @@ import ( "iter" "maps" "reflect" + "strings" "github.com/google/jsonschema-go/jsonschema" ) @@ -81,6 +82,10 @@ func convertFromMap[T any](m map[string]any, strict, tool bool) (T, error) { for name, field := range foreachField(&val) { f, ok := m[name] if !ok { + fieldType, _ := reflect.TypeFor[T]().FieldByName(name) + if strings.Contains(fieldType.Tag.Get("json"), ",omitempty") { + continue + } if tool { return val, BadCallError(fmt.Sprintf("missing argument %q", name)) } else { diff --git a/pkg/aflow/schema_test.go b/pkg/aflow/schema_test.go index b5ed1062f..5adef3776 100644 --- a/pkg/aflow/schema_test.go +++ b/pkg/aflow/schema_test.go @@ -79,10 +79,10 @@ func TestConvertFromMap(t *testing.T) { testConvertFromMap(t, true, map[string]any{ "I1": 2.0, }, struct { - I0 int + I0 int `json:"I0"` }{}, `missing argument "I0"`, - `field "I0" is not present when converting map to struct { I0 int }`) + `field "I0" is not present when converting map to struct { I0 int "json:\"I0\"" }`) testConvertFromMap(t, true, map[string]any{ "I0": "foo", @@ -108,6 +108,14 @@ func TestConvertFromMap(t *testing.T) { }{}, `unused fields when converting map to struct { I0 int }: map[I1:2]`, `unused fields when converting map to struct { I0 int }: map[I1:2]`) + + testConvertFromMap(t, false, map[string]any{ + "I1": 2.0, + }, struct { + I0 int `json:",omitempty"` + }{}, + ``, + ``) } func testConvertFromMap[T any](t *testing.T, strict bool, input map[string]any, output T, toolErr, nonToolErr string) { -- cgit mrf-deployment