mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-29 12:11:02 +00:00
all: statically enforce json/v2 interface satisfaction (#15154)
The json/v2 prototype is still in flux and the API can/will change. Statically enforce that types implementing the v2 methods satisfy the correct interface so that changes to the signature can be statically detected by the compiler. Updates tailscale/corp#791 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
parent
f5522e62d1
commit
3d28aa19cb
@ -13,6 +13,11 @@ import (
|
|||||||
"tailscale.com/util/must"
|
"tailscale.com/util/must"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*Value[bool])(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*Value[bool])(nil)
|
||||||
|
)
|
||||||
|
|
||||||
type testStruct struct {
|
type testStruct struct {
|
||||||
Int int `json:",omitempty,omitzero"`
|
Int int `json:",omitempty,omitzero"`
|
||||||
Str string `json:",omitempty"`
|
Str string `json:",omitempty"`
|
||||||
|
@ -157,6 +157,11 @@ func (lv ListView[T]) Equal(lv2 ListView[T]) bool {
|
|||||||
return lv.ж.Equal(*lv2.ж)
|
return lv.ж.Equal(*lv2.ж)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*ListView[bool])(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*ListView[bool])(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
func (lv ListView[T]) MarshalJSONTo(out *jsontext.Encoder) error {
|
func (lv ListView[T]) MarshalJSONTo(out *jsontext.Encoder) error {
|
||||||
return lv.ж.MarshalJSONTo(out)
|
return lv.ж.MarshalJSONTo(out)
|
||||||
|
@ -158,6 +158,11 @@ func (p *preference[T]) SetReadOnly(readonly bool) {
|
|||||||
p.s.Metadata.ReadOnly = readonly
|
p.s.Metadata.ReadOnly = readonly
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*preference[struct{}])(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*preference[struct{}])(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
func (p preference[T]) MarshalJSONTo(out *jsontext.Encoder) error {
|
func (p preference[T]) MarshalJSONTo(out *jsontext.Encoder) error {
|
||||||
return jsonv2.MarshalEncode(out, &p.s)
|
return jsonv2.MarshalEncode(out, &p.s)
|
||||||
|
@ -128,6 +128,11 @@ type AppConnectorPrefs struct {
|
|||||||
Advertise prefs.Item[bool] `json:",omitzero"`
|
Advertise prefs.Item[bool] `json:",omitzero"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*Prefs)(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*Prefs)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
// It is implemented as a performance improvement and to enable omission of
|
// It is implemented as a performance improvement and to enable omission of
|
||||||
// unconfigured preferences from the JSON output. See the [Prefs] doc for details.
|
// unconfigured preferences from the JSON output. See the [Prefs] doc for details.
|
||||||
|
@ -19,6 +19,20 @@ import (
|
|||||||
|
|
||||||
//go:generate go run tailscale.com/cmd/viewer --tags=test --type=TestPrefs,TestBundle,TestValueStruct,TestGenericStruct,TestPrefsGroup
|
//go:generate go run tailscale.com/cmd/viewer --tags=test --type=TestPrefs,TestBundle,TestValueStruct,TestGenericStruct,TestPrefsGroup
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*ItemView[*TestBundle, TestBundleView])(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*ItemView[*TestBundle, TestBundleView])(nil)
|
||||||
|
|
||||||
|
_ jsonv2.MarshalerTo = (*MapView[string, string])(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*MapView[string, string])(nil)
|
||||||
|
|
||||||
|
_ jsonv2.MarshalerTo = (*StructListView[*TestBundle, TestBundleView])(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*StructListView[*TestBundle, TestBundleView])(nil)
|
||||||
|
|
||||||
|
_ jsonv2.MarshalerTo = (*StructMapView[string, *TestBundle, TestBundleView])(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*StructMapView[string, *TestBundle, TestBundleView])(nil)
|
||||||
|
)
|
||||||
|
|
||||||
type TestPrefs struct {
|
type TestPrefs struct {
|
||||||
Int32Item Item[int32] `json:",omitzero"`
|
Int32Item Item[int32] `json:",omitzero"`
|
||||||
UInt64Item Item[uint64] `json:",omitzero"`
|
UInt64Item Item[uint64] `json:",omitzero"`
|
||||||
@ -53,6 +67,11 @@ type TestPrefs struct {
|
|||||||
Group TestPrefsGroup `json:",omitzero"`
|
Group TestPrefsGroup `json:",omitzero"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*TestPrefs)(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*TestPrefs)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
func (p TestPrefs) MarshalJSONTo(out *jsontext.Encoder) error {
|
func (p TestPrefs) MarshalJSONTo(out *jsontext.Encoder) error {
|
||||||
// The testPrefs type shadows the TestPrefs's method set,
|
// The testPrefs type shadows the TestPrefs's method set,
|
||||||
|
@ -50,6 +50,11 @@ func (s Origin) String() string {
|
|||||||
return s.Scope().String()
|
return s.Scope().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*Origin)(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*Origin)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
func (s Origin) MarshalJSONTo(out *jsontext.Encoder) error {
|
func (s Origin) MarshalJSONTo(out *jsontext.Encoder) error {
|
||||||
return jsonv2.MarshalEncode(out, &s.data)
|
return jsonv2.MarshalEncode(out, &s.data)
|
||||||
|
@ -75,6 +75,11 @@ func (i RawItem) String() string {
|
|||||||
return fmt.Sprintf("%v%s", i.data.Value.Value, suffix)
|
return fmt.Sprintf("%v%s", i.data.Value.Value, suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*RawItem)(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*RawItem)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
func (i RawItem) MarshalJSONTo(out *jsontext.Encoder) error {
|
func (i RawItem) MarshalJSONTo(out *jsontext.Encoder) error {
|
||||||
return jsonv2.MarshalEncode(out, &i.data)
|
return jsonv2.MarshalEncode(out, &i.data)
|
||||||
@ -114,6 +119,11 @@ func RawValueOf[T RawValueType](v T) RawValue {
|
|||||||
return RawValue{opt.ValueOf[any](v)}
|
return RawValue{opt.ValueOf[any](v)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*RawValue)(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*RawValue)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
func (v RawValue) MarshalJSONTo(out *jsontext.Encoder) error {
|
func (v RawValue) MarshalJSONTo(out *jsontext.Encoder) error {
|
||||||
return jsonv2.MarshalEncode(out, v.Value)
|
return jsonv2.MarshalEncode(out, v.Value)
|
||||||
|
@ -147,6 +147,11 @@ type snapshotJSON struct {
|
|||||||
Settings map[Key]RawItem `json:",omitempty"`
|
Settings map[Key]RawItem `json:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*Snapshot)(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*Snapshot)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
func (s *Snapshot) MarshalJSONTo(out *jsontext.Encoder) error {
|
func (s *Snapshot) MarshalJSONTo(out *jsontext.Encoder) error {
|
||||||
data := &snapshotJSON{}
|
data := &snapshotJSON{}
|
||||||
|
@ -54,6 +54,11 @@ func (s Summary) String() string {
|
|||||||
return s.data.Scope.String()
|
return s.data.Scope.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ jsonv2.MarshalerTo = (*Summary)(nil)
|
||||||
|
_ jsonv2.UnmarshalerFrom = (*Summary)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
// MarshalJSONTo implements [jsonv2.MarshalerTo].
|
||||||
func (s Summary) MarshalJSONTo(out *jsontext.Encoder) error {
|
func (s Summary) MarshalJSONTo(out *jsontext.Encoder) error {
|
||||||
return jsonv2.MarshalEncode(out, &s.data)
|
return jsonv2.MarshalEncode(out, &s.data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user