types/geo: add support for ScalarMarshaler and ScalarUnmarshaler (#20158)
Some checks failed
checklocks / checklocks (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
Dockerfile build / deploy (push) Has been cancelled
natlab-basic / EasyEasy (push) Has been cancelled
CI / gomod-cache (push) Has been cancelled
CI / fuzz (push) Has been cancelled
tailscale.com/cmd/vet / vet (push) Has been cancelled
CI / race-root-integration (1/4) (push) Has been cancelled
CI / race-root-integration (2/4) (push) Has been cancelled
CI / race-root-integration (3/4) (push) Has been cancelled
CI / race-root-integration (4/4) (push) Has been cancelled
CI / test (-race, amd64, 1/3) (push) Has been cancelled
CI / test (-race, amd64, 2/3) (push) Has been cancelled
CI / test (-race, amd64, 3/3) (push) Has been cancelled
CI / test (386) (push) Has been cancelled
CI / test (amd64) (push) Has been cancelled
CI / Windows (${{ matrix.name || matrix.shard}}) (win-bench, benchmarks) (push) Has been cancelled
CI / Windows (${{ matrix.name || matrix.shard}}) (win-shard-1-2, 1/2) (push) Has been cancelled
CI / Windows (${{ matrix.name || matrix.shard}}) (win-shard-2-2, 2/2) (push) Has been cancelled
CI / macos (push) Has been cancelled
CI / privileged (push) Has been cancelled
CI / vm (push) Has been cancelled
CI / cross (386, linux) (push) Has been cancelled
CI / cross (amd64, darwin) (push) Has been cancelled
CI / cross (amd64, freebsd) (push) Has been cancelled
CI / cross (amd64, openbsd) (push) Has been cancelled
CI / cross (amd64, windows) (push) Has been cancelled
CI / cross (arm, 5, linux) (push) Has been cancelled
CI / cross (arm, 7, linux) (push) Has been cancelled
CI / cross (arm64, darwin) (push) Has been cancelled
CI / cross (arm64, linux) (push) Has been cancelled
CI / cross (arm64, windows) (push) Has been cancelled
CI / cross (loong64, linux) (push) Has been cancelled
CI / ios (push) Has been cancelled
CI / crossmin (amd64, illumos) (push) Has been cancelled
CI / crossmin (amd64, plan9) (push) Has been cancelled
CI / crossmin (amd64, solaris) (push) Has been cancelled
CI / crossmin (ppc64, aix) (push) Has been cancelled
CI / android (push) Has been cancelled
CI / wasm (push) Has been cancelled
CI / tailscale_go (push) Has been cancelled
CI / depaware (push) Has been cancelled
CI / go_generate (push) Has been cancelled
CI / make_tidy (push) Has been cancelled
CI / licenses (push) Has been cancelled
CI / staticcheck (${{ matrix.name }}) (--with-tags-all=darwin, arm64, darwin, macOS) (push) Has been cancelled
CI / staticcheck (${{ matrix.name }}) (--with-tags-all=linux, amd64, linux, Linux) (push) Has been cancelled
CI / staticcheck (${{ matrix.name }}) (--with-tags-all=windows, amd64, windows, Windows) (push) Has been cancelled
CI / staticcheck (${{ matrix.name }}) (--without-tags-any=windows,darwin,linux --shard=1/4, amd64, linux, Portable (1/4)) (push) Has been cancelled
CI / staticcheck (${{ matrix.name }}) (--without-tags-any=windows,darwin,linux --shard=2/4, amd64, linux, Portable (2/4)) (push) Has been cancelled
CI / staticcheck (${{ matrix.name }}) (--without-tags-any=windows,darwin,linux --shard=3/4, amd64, linux, Portable (3/4)) (push) Has been cancelled
CI / staticcheck (${{ matrix.name }}) (--without-tags-any=windows,darwin,linux --shard=4/4, amd64, linux, Portable (4/4)) (push) Has been cancelled
CI / notify_slack (push) Has been cancelled
CI / merge_blocker (push) Has been cancelled
CI / check_mergeability_strict (push) Has been cancelled
CI / check_mergeability (push) Has been cancelled

Add support for the still pending encoding.ScalarMarshaler and
encoding.ScalarUnmarshaler interfaces, approved in golang/go#56235.

This patch deprecates geo.Point.MarshalUint64 in favour of
geo.Point.MarshalScalar and also adds an inline directive for go fix.
The same applies for the UnmarshalUint64 and UnmarshalScalar methods.

Updates #16583

Signed-off-by: Simon Law <sfllaw@tailscale.com>
This commit is contained in:
Simon Law 2026-06-16 16:36:43 -07:00 committed by GitHub
parent f0a1aa818f
commit 88f5206511
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 11 deletions

View File

@ -166,7 +166,7 @@ func (p Point) AppendBinary(b []byte) ([]byte, error) {
return b, nil
}
// MarshalBinary implements [encoding.BinaryMarshaller]. The output matches that
// MarshalBinary implements [encoding.BinaryMarshaler]. The output matches that
// of calling [Point.AppendBinary].
func (p Point) MarshalBinary() ([]byte, error) {
var b [8]byte
@ -223,25 +223,43 @@ func (p Point) AppendText(b []byte) ([]byte, error) {
return b, nil
}
// MarshalText implements [encoding.TextMarshaller]. The output matches that
// MarshalText implements [encoding.TextMarshaler]. The output matches that
// of calling [Point.AppendText].
func (p Point) MarshalText() ([]byte, error) {
var b [8]byte
return p.AppendText(b[:0])
}
// MarshalUint64 produces the same output as MashalBinary, encoded in a uint64.
func (p Point) MarshalUint64() (uint64, error) {
// MarshalScalar implements [encoding.ScalarMarshaler].
// It produces the same output as MashalBinary, encoded in a uint64.
func (p Point) MarshalScalar() (uint64, error) {
b, err := p.MarshalBinary()
return binary.NativeEndian.Uint64(b), err
}
// UnmarshalUint64 expects input formatted by MarshalUint64.
func (p *Point) UnmarshalUint64(v uint64) error {
// UnmarshalScalar implements [encoding.ScalarUnmarshaler].
// It expects input formatted by MarshalScalar.
func (p *Point) UnmarshalScalar(v uint64) error {
b := binary.NativeEndian.AppendUint64(nil, v)
return p.UnmarshalBinary(b)
}
// MarshalUint64 produces the same output as MashalBinary, encoded in a uint64.
// Deprecated: this function simply calls [Point.MarshalScalar].
//
//go:fix inline
func (p Point) MarshalUint64() (uint64, error) {
return p.MarshalScalar()
}
// UnmarshalUint64 expects input formatted by MarshalUint64.
// Deprecated: this function simply calls [Point.UnmarshalScalar].
//
//go:fix inline
func (p *Point) UnmarshalUint64(v uint64) error {
return p.UnmarshalScalar(v)
}
// IsZero reports if p is the zero value.
func (p Point) IsZero() bool {
return p == Point{}

View File

@ -41,7 +41,7 @@ func TestPointZero(t *testing.T) {
}
wantI := uint64(0x00000000)
if i, err := zero.MarshalUint64(); err != nil {
if i, err := zero.MarshalScalar(); err != nil {
t.Errorf("MarshalUint64() err %q, want nil", err)
} else if i != wantI {
t.Errorf("MarshalUint64 got %v, want %v", i, wantI)
@ -358,13 +358,13 @@ func TestPoint(t *testing.T) {
t.Errorf("UnmarshalBinary: roundtrip failed: %#v != %#v", q, p)
}
i, err := p.MarshalUint64()
i, err := p.MarshalScalar()
if err != nil {
t.Fatalf("MarshalUint64: err %q, expected nil", err)
}
var r geo.Point
if err := r.UnmarshalUint64(i); err != nil {
if err := r.UnmarshalScalar(i); err != nil {
t.Fatalf("UnmarshalUint64: err %r, expected nil", err)
}
if !q.EqualApprox(r, -1) {
@ -414,12 +414,12 @@ func TestPointMarshalBinary(t *testing.T) {
func TestPointMarshalUint64(t *testing.T) {
t.Skip("skip")
roundtrip := func(p geo.Point) error {
i, err := p.MarshalUint64()
i, err := p.MarshalScalar()
if err != nil {
return fmt.Errorf("marshal: %v", err)
}
var q geo.Point
if err := q.UnmarshalUint64(i); err != nil {
if err := q.UnmarshalScalar(i); err != nil {
return fmt.Errorf("unmarshal: %v", err)
}
if q != p {