From d8b070338fcbb3faab61eee5767e7427c1901148 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 26 Jan 2025 15:55:10 +0800 Subject: [PATCH] Enhance tag parsing flexibility in Assistant loading - Improved tag handling in loadMap to support multiple input types - Added support for parsing tags from []interface{}, string, and other JSON-compatible formats - Utilized jsoniter for robust type conversion and marshaling - Increased robustness of tag loading process for diverse input scenarios This change enhances the flexibility of tag parsing during Assistant initialization, allowing more versatile data input methods. --- neo/assistant/load.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/neo/assistant/load.go b/neo/assistant/load.go index 7fd638f1..56f166bf 100644 --- a/neo/assistant/load.go +++ b/neo/assistant/load.go @@ -365,8 +365,32 @@ func loadMap(data map[string]interface{}) (*Assistant, error) { } // tags - if v, ok := data["tags"].([]string); ok { - assistant.Tags = v + if v, has := data["tags"]; has { + switch vv := v.(type) { + case []string: + assistant.Tags = vv + case []interface{}: + var tags []string + for _, tag := range vv { + tags = append(tags, cast.ToString(tag)) + } + assistant.Tags = tags + + case interface{}: + raw, err := jsoniter.Marshal(vv) + if err != nil { + return nil, err + } + var tags []string + err = jsoniter.Unmarshal(raw, &tags) + if err != nil { + return nil, err + } + assistant.Tags = tags + + case string: + assistant.Tags = []string{vv} + } } // options