From fdca9b3f8e355dcf0caa27a6207109406e7be4bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 16 Nov 2024 12:16:33 +0800 Subject: [PATCH] badjson: Fix Listable --- common/json/badjson/merge.go | 2 +- common/json/badoption/listable.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/common/json/badjson/merge.go b/common/json/badjson/merge.go index ac1f12fe..30bc74d8 100644 --- a/common/json/badjson/merge.go +++ b/common/json/badjson/merge.go @@ -11,7 +11,7 @@ import ( ) func Omitempty[T any](ctx context.Context, value T) (T, error) { - objectContent, err := json.Marshal(value) + objectContent, err := json.MarshalContext(ctx, value) if err != nil { return common.DefaultValue[T](), E.Cause(err, "marshal object") } diff --git a/common/json/badoption/listable.go b/common/json/badoption/listable.go index 60a044c8..df02217e 100644 --- a/common/json/badoption/listable.go +++ b/common/json/badoption/listable.go @@ -9,24 +9,24 @@ import ( type Listable[T any] []T -func (l Listable[T]) MarshalJSON() ([]byte, error) { +func (l Listable[T]) MarshalJSONContext(ctx context.Context) ([]byte, error) { arrayList := []T(l) if len(arrayList) == 1 { return json.Marshal(arrayList[0]) } - return json.Marshal(arrayList) + return json.MarshalContext(ctx, arrayList) } func (l *Listable[T]) UnmarshalJSONContext(ctx context.Context, content []byte) error { - err := json.UnmarshalContextDisallowUnknownFields(ctx, content, (*[]T)(l)) + var singleItem T + err := json.UnmarshalContextDisallowUnknownFields(ctx, content, &singleItem) if err == nil { + *l = []T{singleItem} return nil } - var singleItem T - newError := json.UnmarshalContextDisallowUnknownFields(ctx, content, &singleItem) - if newError != nil { - return E.Errors(err, newError) + newErr := json.UnmarshalContextDisallowUnknownFields(ctx, content, (*[]T)(l)) + if newErr == nil { + return nil } - *l = []T{singleItem} - return nil + return E.Errors(err, newErr) }