From 1354a2c9a31f6cd8a5c994ffdbcc98432ea11f38 Mon Sep 17 00:00:00 2001 From: Yonah Date: Sun, 22 Mar 2026 16:34:51 +0900 Subject: [PATCH] ident: member interface For representing full identifiers of system and member. Signed-off-by: Yonah --- ident/ident.go | 20 ++++++++++++++++++++ ident/ident_test.go | 2 +- ident/member.go | 5 +++++ ident/system.go | 5 +++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ident/ident.go b/ident/ident.go index bfcb948..b577d10 100644 --- a/ident/ident.go +++ b/ident/ident.go @@ -3,7 +3,9 @@ package ident import ( + "encoding" "errors" + "fmt" "strconv" ) @@ -21,3 +23,21 @@ func (e *UnexpectedSizeError) Error() string { return "got " + strconv.Itoa(len(e.Data)) + " bytes for " + "a " + strconv.Itoa(e.Want) + "-byte identifier" } + +// MS is a system or member identifier. +type MS interface { + encoding.TextMarshaler + encoding.TextUnmarshaler + fmt.Stringer + + // EncodedSize returns the number of bytes appended by Encode. + EncodedSize() int + // Encode appends the canonical string representation of MS to dst and + // returns the extended buffer. + Encode(dst []byte) []byte + + // ident is a no-op function but serves to distinguish types that are + // identifiers from ordinary types with custom marshaler/unmarshaler + // behaviour: a type is an identifier part if it has an ident method. + ident() +} diff --git a/ident/ident_test.go b/ident/ident_test.go index 241d031..5edd6a5 100644 --- a/ident/ident_test.go +++ b/ident/ident_test.go @@ -69,7 +69,7 @@ func (testCases rTestCases[V, S]) run(t *testing.T) { } if got := S(&tc.ident).String(); got != string(tc.want) { - t.Errorf("String: %#v, want %#v", []byte(got), tc.want) + t.Errorf("String: %s, want %s", got, tc.want) } }) }) diff --git a/ident/member.go b/ident/member.go index d4b0eb6..9d727f0 100644 --- a/ident/member.go +++ b/ident/member.go @@ -23,6 +23,8 @@ type M struct { System S } +func (*M) ident() {} + const ( // SizeMember is the size of the binary representation of [M]. SizeMember = SizeSystem * 2 @@ -30,6 +32,9 @@ const ( EncodedSizeMember = SizeMember / 3 * 4 ) +// EncodedSize returns the number of bytes appended by Encode. +func (*M) EncodedSize() int { return EncodedSizeMember } + // Encode appends the canonical string representation of mid to dst and returns // the extended buffer. func (mid *M) Encode(dst []byte) []byte { diff --git a/ident/system.go b/ident/system.go index 5d98c53..c8d2b97 100644 --- a/ident/system.go +++ b/ident/system.go @@ -31,6 +31,8 @@ type S struct { ID uint64 } +func (*S) ident() {} + const ( // SizeSystem is the size of the binary representation of [S]. SizeSystem = 24 @@ -38,6 +40,9 @@ const ( EncodedSizeSystem = SizeSystem / 3 * 4 ) +// EncodedSize returns the number of bytes appended by Encode. +func (*S) EncodedSize() int { return EncodedSizeSystem } + // Encode appends the canonical string representation of sid to dst and returns // the extended buffer. func (sid *S) Encode(dst []byte) []byte {