github.com/anthropics/anthropic-sdk-go@v1.70.1/unmarshalcompat.go (about) 1 package anthropic 2 3 // To accept the API's `string | []T` form, register string promotion 4 // for beta content unions. The generator already covers the non-beta 5 // ones (message.go init). 6 7 import ( 8 "reflect" 9 10 "github.com/anthropics/anthropic-sdk-go/internal/apijson" 11 12 "github.com/tidwall/gjson" 13 ) 14 15 func registerStringPromotion[SliceT ~[]E, E any](wrap func(string) E) { 16 apijson.RegisterCustomDecoder[SliceT](func(node gjson.Result, value reflect.Value, defaultDecoder func(gjson.Result, reflect.Value) error) error { 17 if node.Type == gjson.String { 18 arrayValue := reflect.MakeSlice(value.Type(), 1, 1) 19 arrayValue.Index(0).Set(reflect.ValueOf(wrap(node.String()))) 20 value.Set(arrayValue) 21 return nil 22 } 23 return defaultDecoder(node, value) 24 }) 25 } 26 27 func init() { 28 registerStringPromotion[[]TextBlockParam](func(s string) TextBlockParam { 29 return TextBlockParam{Text: s} 30 }) 31 registerStringPromotion[[]BetaTextBlockParam](func(s string) BetaTextBlockParam { 32 return BetaTextBlockParam{Text: s} 33 }) 34 registerStringPromotion[[]BetaContentBlockParamUnion](func(s string) BetaContentBlockParamUnion { 35 return BetaContentBlockParamUnion{OfText: &BetaTextBlockParam{Text: s}} 36 }) 37 registerStringPromotion[[]BetaToolResultBlockParamContentUnion](func(s string) BetaToolResultBlockParamContentUnion { 38 return BetaToolResultBlockParamContentUnion{OfText: &BetaTextBlockParam{Text: s}} 39 }) 40 }