Skip to content

Commit 9ea70da

Browse files
feat(client): add a convenient param.SetJSON helper
1 parent 279946e commit 9ea70da

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

packages/param/param.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ func Override[T ParamStruct, PtrT InferPtr[T]](v any) T {
4141
return *pt
4242
}
4343

44+
// SetJSON configures a param struct to serialize with the provided raw JSON data.
45+
// Use this when you have existing JSON that you want to send as request parameters.
46+
//
47+
// var req example.NewUserParams
48+
// var rawJSON = []byte(`{"name": "...", "age": 40}`)
49+
// param.SetJSON(rawJSON, &req)
50+
// res, err := client.Users.New(ctx, req)
51+
//
52+
// Note: The struct's existing fields will be ignored; only the provided JSON is serialized.
53+
func SetJSON(rawJSON []byte, ptr anyParamStruct) {
54+
ptr.setMetadata(json.RawMessage(rawJSON))
55+
}
56+
4457
// IsOmitted returns true if v is the zero value of its type.
4558
//
4659
// If IsOmitted is true, and the field uses a `json:"...,omitzero"` tag,
@@ -91,6 +104,11 @@ type ParamStruct interface {
91104
extraFields() map[string]any
92105
}
93106

107+
// A pointer to ParamStruct
108+
type anyParamStruct interface {
109+
setMetadata(any)
110+
}
111+
94112
// This is an implementation detail and should never be explicitly set.
95113
type InferPtr[T ParamStruct] interface {
96114
setMetadata(any)

0 commit comments

Comments
 (0)