Skip to content

Commit 21d3e9f

Browse files
committedFeb 5, 2025·
chore: add UnionUnmarshaler for responses that are interfaces (#3897)
1 parent 94c1ff8 commit 21d3e9f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
 

‎internal/apijson/registry.go

+10
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,13 @@ func RegisterUnion(typ reflect.Type, discriminator string, variants ...UnionVari
2929
unionVariants[variant.Type] = typ
3030
}
3131
}
32+
33+
// Useful to wrap a union type to force it to use [apijson.UnmarshalJSON] since you cannot define an
34+
// UnmarshalJSON function on the interface itself.
35+
type UnionUnmarshaler[T any] struct {
36+
Value T
37+
}
38+
39+
func (c *UnionUnmarshaler[T]) UnmarshalJSON(buf []byte) error {
40+
return UnmarshalRoot(buf, &c.Value)
41+
}

‎logs/rayid.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"net/url"
1111

12+
"github.com/cloudflare/cloudflare-go/v4/internal/apijson"
1213
"github.com/cloudflare/cloudflare-go/v4/internal/apiquery"
1314
"github.com/cloudflare/cloudflare-go/v4/internal/param"
1415
"github.com/cloudflare/cloudflare-go/v4/internal/requestconfig"
@@ -37,6 +38,7 @@ func NewRayIDService(opts ...option.RequestOption) (r *RayIDService) {
3738
// The `/rayids` api route allows lookups by specific rayid. The rayids route will
3839
// return zero, one, or more records (ray ids are not unique).
3940
func (r *RayIDService) Get(ctx context.Context, RayID string, params RayIDGetParams, opts ...option.RequestOption) (res *interface{}, err error) {
41+
var env apijson.UnionUnmarshaler[interface{}]
4042
opts = append(r.Options[:], opts...)
4143
if params.ZoneID.Value == "" {
4244
err = errors.New("missing required zone_id parameter")
@@ -47,7 +49,11 @@ func (r *RayIDService) Get(ctx context.Context, RayID string, params RayIDGetPar
4749
return
4850
}
4951
path := fmt.Sprintf("zones/%s/logs/rayids/%s", params.ZoneID, RayID)
50-
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, params, &res, opts...)
52+
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, params, &env, opts...)
53+
if err != nil {
54+
return
55+
}
56+
res = &env.Value
5157
return
5258
}
5359

‎logs/received.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"net/url"
1111

12+
"github.com/cloudflare/cloudflare-go/v4/internal/apijson"
1213
"github.com/cloudflare/cloudflare-go/v4/internal/apiquery"
1314
"github.com/cloudflare/cloudflare-go/v4/internal/param"
1415
"github.com/cloudflare/cloudflare-go/v4/internal/requestconfig"
@@ -45,13 +46,18 @@ func NewReceivedService(opts ...option.RequestOption) (r *ReceivedService) {
4546
// `start=2018-05-20T10:01:00Z&end=2018-05-20T10:02:00Z` and so on; the overlap
4647
// will be handled properly.
4748
func (r *ReceivedService) Get(ctx context.Context, params ReceivedGetParams, opts ...option.RequestOption) (res *interface{}, err error) {
49+
var env apijson.UnionUnmarshaler[interface{}]
4850
opts = append(r.Options[:], opts...)
4951
if params.ZoneID.Value == "" {
5052
err = errors.New("missing required zone_id parameter")
5153
return
5254
}
5355
path := fmt.Sprintf("zones/%s/logs/received", params.ZoneID)
54-
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, params, &res, opts...)
56+
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, params, &env, opts...)
57+
if err != nil {
58+
return
59+
}
60+
res = &env.Value
5561
return
5662
}
5763

0 commit comments

Comments
 (0)
Please sign in to comment.