diff options
Diffstat (limited to 'check/absolute.go')
| -rw-r--r-- | check/absolute.go | 45 |
1 files changed, 22 insertions, 23 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) { |
