From 67db3fbb8d7ba95071e50c43e96a0dd79e55a476 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Fri, 10 Apr 2026 22:11:53 +0900 Subject: [PATCH] check: use encoding interfaces This turned out not to require specific treatment, so the shared interfaces are cleaner. Signed-off-by: Ophestra --- check/absolute.go | 45 +++++++++++++++++++++--------------------- check/absolute_test.go | 21 ++++++-------------- 2 files changed, 28 insertions(+), 38 deletions(-) diff --git a/check/absolute.go b/check/absolute.go index bf6ed813..4df3c59b 100644 --- a/check/absolute.go +++ b/check/absolute.go @@ -2,7 +2,7 @@ package check import ( - "encoding/json" + "encoding" "errors" "fmt" "path/filepath" @@ -30,6 +30,16 @@ func (e AbsoluteError) Is(target error) bool { // Absolute holds a pathname checked to be absolute. type Absolute struct{ pathname unique.Handle[string] } +var ( + _ encoding.TextAppender = new(Absolute) + _ encoding.TextMarshaler = new(Absolute) + _ encoding.TextUnmarshaler = new(Absolute) + + _ encoding.BinaryAppender = new(Absolute) + _ encoding.BinaryMarshaler = new(Absolute) + _ encoding.BinaryUnmarshaler = new(Absolute) +) + // ok returns whether [Absolute] is not the zero value. func (a *Absolute) ok() bool { return a != nil && *a != (Absolute{}) } @@ -84,13 +94,16 @@ func (a *Absolute) Append(elem ...string) *Absolute { // Dir calls [filepath.Dir] with [Absolute] as its argument. func (a *Absolute) Dir() *Absolute { return unsafeAbs(filepath.Dir(a.String())) } -// GobEncode returns the checked pathname. -func (a *Absolute) GobEncode() ([]byte, error) { - return []byte(a.String()), nil +// AppendText appends the checked pathname. +func (a *Absolute) AppendText(data []byte) ([]byte, error) { + return append(data, a.String()...), nil } -// GobDecode stores data if it represents an absolute pathname. -func (a *Absolute) GobDecode(data []byte) error { +// MarshalText returns the checked pathname. +func (a *Absolute) MarshalText() ([]byte, error) { return a.AppendText(nil) } + +// UnmarshalText stores data if it represents an absolute pathname. +func (a *Absolute) UnmarshalText(data []byte) error { pathname := string(data) if !filepath.IsAbs(pathname) { return AbsoluteError(pathname) @@ -99,23 +112,9 @@ func (a *Absolute) GobDecode(data []byte) error { return nil } -// MarshalJSON returns a JSON representation of the checked pathname. -func (a *Absolute) MarshalJSON() ([]byte, error) { - return json.Marshal(a.String()) -} - -// UnmarshalJSON stores data if it represents an absolute pathname. -func (a *Absolute) UnmarshalJSON(data []byte) error { - var pathname string - if err := json.Unmarshal(data, &pathname); err != nil { - return err - } - if !filepath.IsAbs(pathname) { - return AbsoluteError(pathname) - } - a.pathname = unique.Make(pathname) - return nil -} +func (a *Absolute) AppendBinary(data []byte) ([]byte, error) { return a.AppendText(data) } +func (a *Absolute) MarshalBinary() ([]byte, error) { return a.MarshalText() } +func (a *Absolute) UnmarshalBinary(data []byte) error { return a.UnmarshalText(data) } // SortAbs calls [slices.SortFunc] for a slice of [Absolute]. func SortAbs(x []*Absolute) { diff --git a/check/absolute_test.go b/check/absolute_test.go index 445ff2e2..8d4ca353 100644 --- a/check/absolute_test.go +++ b/check/absolute_test.go @@ -170,20 +170,20 @@ func TestCodecAbsolute(t *testing.T) { {"good", MustAbs("/etc"), nil, - "\t\x7f\x05\x01\x02\xff\x82\x00\x00\x00\b\xff\x80\x00\x04/etc", - ",\xff\x83\x03\x01\x01\x06sCheck\x01\xff\x84\x00\x01\x02\x01\bPathname\x01\xff\x80\x00\x01\x05Magic\x01\x06\x00\x00\x00\t\x7f\x05\x01\x02\xff\x82\x00\x00\x00\x0f\xff\x84\x01\x04/etc\x01\xfc\xc0\xed\x00\x00\x00", + "\t\x7f\x06\x01\x02\xff\x82\x00\x00\x00\b\xff\x80\x00\x04/etc", + ",\xff\x83\x03\x01\x01\x06sCheck\x01\xff\x84\x00\x01\x02\x01\bPathname\x01\xff\x80\x00\x01\x05Magic\x01\x06\x00\x00\x00\t\x7f\x06\x01\x02\xff\x82\x00\x00\x00\x0f\xff\x84\x01\x04/etc\x01\xfc\xc0\xed\x00\x00\x00", `"/etc"`, `{"val":"/etc","magic":3236757504}`}, {"not absolute", nil, AbsoluteError("etc"), - "\t\x7f\x05\x01\x02\xff\x82\x00\x00\x00\a\xff\x80\x00\x03etc", - ",\xff\x83\x03\x01\x01\x06sCheck\x01\xff\x84\x00\x01\x02\x01\bPathname\x01\xff\x80\x00\x01\x05Magic\x01\x06\x00\x00\x00\t\x7f\x05\x01\x02\xff\x82\x00\x00\x00\x0f\xff\x84\x01\x03etc\x01\xfb\x01\x81\xda\x00\x00\x00", + "\t\x7f\x06\x01\x02\xff\x82\x00\x00\x00\a\xff\x80\x00\x03etc", + ",\xff\x83\x03\x01\x01\x06sCheck\x01\xff\x84\x00\x01\x02\x01\bPathname\x01\xff\x80\x00\x01\x05Magic\x01\x06\x00\x00\x00\t\x7f\x06\x01\x02\xff\x82\x00\x00\x00\x0f\xff\x84\x01\x03etc\x01\xfb\x01\x81\xda\x00\x00\x00", `"etc"`, `{"val":"etc","magic":3236757504}`}, {"zero", nil, new(AbsoluteError), - "\t\x7f\x05\x01\x02\xff\x82\x00\x00\x00\x04\xff\x80\x00\x00", - ",\xff\x83\x03\x01\x01\x06sCheck\x01\xff\x84\x00\x01\x02\x01\bPathname\x01\xff\x80\x00\x01\x05Magic\x01\x06\x00\x00\x00\t\x7f\x05\x01\x02\xff\x82\x00\x00\x00\f\xff\x84\x01\x00\x01\xfb\x01\x81\xda\x00\x00\x00", + "\t\x7f\x06\x01\x02\xff\x82\x00\x00\x00\x04\xff\x80\x00\x00", + ",\xff\x83\x03\x01\x01\x06sCheck\x01\xff\x84\x00\x01\x02\x01\bPathname\x01\xff\x80\x00\x01\x05Magic\x01\x06\x00\x00\x00\t\x7f\x06\x01\x02\xff\x82\x00\x00\x00\f\xff\x84\x01\x00\x01\xfb\x01\x81\xda\x00\x00\x00", `""`, `{"val":"","magic":3236757504}`}, } @@ -347,15 +347,6 @@ func TestCodecAbsolute(t *testing.T) { }) }) } - - t.Run("json passthrough", func(t *testing.T) { - t.Parallel() - - wantErr := "invalid character ':' looking for beginning of value" - if err := new(Absolute).UnmarshalJSON([]byte(":3")); err == nil || err.Error() != wantErr { - t.Errorf("UnmarshalJSON: error = %v, want %s", err, wantErr) - } - }) } func TestAbsoluteWrap(t *testing.T) {