From 520a519b257d5de323a61bb0c4acdad7ee49ceb1 Mon Sep 17 00:00:00 2001 From: stainless-bot Date: Wed, 5 Jun 2024 19:54:54 +0000 Subject: [PATCH] chore: update SDK settings --- internal/apijson/decoder.go | 10 ++++++++++ internal/apijson/json_test.go | 20 ++++++++++++++++++++ scripts/format | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/apijson/decoder.go b/internal/apijson/decoder.go index 2c1dd80..f3c4f19 100644 --- a/internal/apijson/decoder.go +++ b/internal/apijson/decoder.go @@ -124,7 +124,14 @@ func (d *decoderBuilder) typeDecoder(t reflect.Type) decoderFunc { return f } +func indirectUnmarshalerDecoder(n gjson.Result, v reflect.Value, state *decoderState) error { + return v.Addr().Interface().(json.Unmarshaler).UnmarshalJSON([]byte(n.Raw)) +} + func unmarshalerDecoder(n gjson.Result, v reflect.Value, state *decoderState) error { + if v.Kind() == reflect.Pointer && v.CanSet() { + v.Set(reflect.New(v.Type().Elem())) + } return v.Interface().(json.Unmarshaler).UnmarshalJSON([]byte(n.Raw)) } @@ -135,6 +142,9 @@ func (d *decoderBuilder) newTypeDecoder(t reflect.Type) decoderFunc { if !d.root && t.Implements(reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()) { return unmarshalerDecoder } + if !d.root && reflect.PointerTo(t).Implements(reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()) { + return indirectUnmarshalerDecoder + } d.root = false if _, ok := unionRegistry[t]; ok { diff --git a/internal/apijson/json_test.go b/internal/apijson/json_test.go index a5b80c0..81cea3b 100644 --- a/internal/apijson/json_test.go +++ b/internal/apijson/json_test.go @@ -202,6 +202,16 @@ func (t TypeB) IsKnown() bool { return t == "b" } +type UnmarshalStruct struct { + Foo string `json:"foo"` + prop bool `json:"-"` +} + +func (r *UnmarshalStruct) UnmarshalJSON(json []byte) error { + r.prop = true + return UnmarshalRoot(json, r) +} + func (ComplexUnionTypeB) complexUnion() {} func init() { @@ -433,6 +443,16 @@ var tests = map[string]struct { ComplexUnionStruct{Union: ComplexUnionTypeB{Baz: 12, Type: TypeB("b")}}, }, + "unmarshal": { + `{"foo":"hello"}`, + &UnmarshalStruct{Foo: "hello", prop: true}, + }, + + "array_of_unmarshal": { + `[{"foo":"hello"}]`, + []UnmarshalStruct{{Foo: "hello", prop: true}}, + }, + "inline_coerce": { `{"a":false,"b":237628372683,"c":654,"d":9999.43,"e":43.76,"f":[1,2,3,4]}`, Inline{ diff --git a/scripts/format b/scripts/format index afcba96..db2a3fa 100755 --- a/scripts/format +++ b/scripts/format @@ -5,4 +5,4 @@ set -e cd "$(dirname "$0")/.." echo "==> Running gofmt -s -w" -gofmt -s -w ./... +gofmt -s -w .