-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(collections): add optional key and value naming methods #20538
Changes from 4 commits
37ccc09
6311f4f
e337c84
85773bf
c4b61b8
7c7ba42
da72a3f
6c4eaf7
ba73fb1
064709a
f2f750c
88a7fc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,11 @@ import ( | |
"strconv" | ||
) | ||
|
||
func NewBoolKey[T ~bool]() KeyCodec[T] { return boolKey[T]{} } | ||
func NewBoolKey[T ~bool]() NameableKeyCodec[T] { return boolKey[T]{} } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined interfaces Please ensure that Also applies to: 70-70 Toolsgolangci-lint
|
||
|
||
type boolKey[T ~bool] struct{} | ||
type boolKey[T ~bool] struct { | ||
name string | ||
} | ||
|
||
func (b boolKey[T]) Encode(buffer []byte, key T) (int, error) { | ||
if key { | ||
|
@@ -64,3 +66,12 @@ func (b boolKey[T]) DecodeNonTerminal(buffer []byte) (int, T, error) { | |
func (b boolKey[T]) SizeNonTerminal(key T) int { | ||
return b.Size(key) | ||
} | ||
|
||
func (b boolKey[T]) WithName(name string) NamedKeyCodec[T] { | ||
b.name = name | ||
return b | ||
} | ||
|
||
func (b boolKey[T]) Name() string { | ||
return b.name | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,11 @@ import ( | |
// using the BytesKey KeyCodec. | ||
const MaxBytesKeyNonTerminalSize = math.MaxUint8 | ||
|
||
func NewBytesKey[T ~[]byte]() KeyCodec[T] { return bytesKey[T]{} } | ||
func NewBytesKey[T ~[]byte]() NameableKeyCodec[T] { return bytesKey[T]{} } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve the undefined type The type Toolsgolangci-lint
|
||
|
||
type bytesKey[T ~[]byte] struct{} | ||
type bytesKey[T ~[]byte] struct { | ||
name string | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure proper encapsulation of the Consider making the - name string
+ name string
|
||
|
||
func (b bytesKey[T]) Encode(buffer []byte, key T) (int, error) { | ||
return copy(buffer, key), nil | ||
|
@@ -77,3 +79,12 @@ func (bytesKey[T]) DecodeNonTerminal(buffer []byte) (int, T, error) { | |
func (bytesKey[T]) SizeNonTerminal(key T) int { | ||
return len(key) + 1 | ||
} | ||
|
||
func (b bytesKey[T]) WithName(name string) NamedKeyCodec[T] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve the undefined type The type Toolsgolangci-lint
|
||
b.name = name | ||
return b | ||
} | ||
|
||
func (b bytesKey[T]) Name() string { | ||
return b.name | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,11 +125,14 @@ type UntypedValueCodec struct { | |
} | ||
|
||
// KeyToValueCodec converts a KeyCodec into a ValueCodec. | ||
func KeyToValueCodec[K any](keyCodec KeyCodec[K]) ValueCodec[K] { return keyToValueCodec[K]{keyCodec} } | ||
func KeyToValueCodec[K any](keyCodec KeyCodec[K]) NameableValueCodec[K] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve the undefined types The types Also applies to: 174-174 Toolsgolangci-lint
|
||
return keyToValueCodec[K]{kc: keyCodec} | ||
} | ||
|
||
// keyToValueCodec is a ValueCodec that wraps a KeyCodec to make it behave like a ValueCodec. | ||
type keyToValueCodec[K any] struct { | ||
kc KeyCodec[K] | ||
name string | ||
kc KeyCodec[K] | ||
} | ||
|
||
func (k keyToValueCodec[K]) EncodeJSON(value K) ([]byte, error) { | ||
|
@@ -167,3 +170,12 @@ func (k keyToValueCodec[K]) Stringify(value K) string { | |
func (k keyToValueCodec[K]) ValueType() string { | ||
return k.kc.KeyType() | ||
} | ||
|
||
func (k keyToValueCodec[K]) WithName(name string) NamedValueCodec[K] { | ||
k.name = name | ||
return k | ||
} | ||
|
||
func (k keyToValueCodec[K]) Name() string { | ||
return k.name | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import ( | |
) | ||
|
||
func TestUntypedValueCodec(t *testing.T) { | ||
vc := NewUntypedValueCodec(KeyToValueCodec(NewStringKeyCodec[string]())) | ||
vc := NewUntypedValueCodec(ValueCodec[string](KeyToValueCodec(KeyCodec[string](NewStringKeyCodec[string]())))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined function Please ensure that Toolsgolangci-lint
|
||
|
||
t.Run("encode/decode", func(t *testing.T) { | ||
_, err := vc.Encode(0) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,11 @@ import ( | |
"strconv" | ||
) | ||
|
||
func NewInt64Key[T ~int64]() KeyCodec[T] { return int64Key[T]{} } | ||
func NewInt64Key[T ~int64]() NameableKeyCodec[T] { return int64Key[T]{} } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve the undefined type The type Also applies to: 78-78 Toolsgolangci-lint
|
||
|
||
type int64Key[T ~int64] struct{} | ||
type int64Key[T ~int64] struct { | ||
name string | ||
} | ||
aaronc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func (i int64Key[T]) Encode(buffer []byte, key T) (int, error) { | ||
binary.BigEndian.PutUint64(buffer, (uint64)(key)) | ||
|
@@ -64,11 +66,22 @@ func (i int64Key[T]) SizeNonTerminal(_ T) int { | |
return 8 | ||
} | ||
|
||
func NewInt32Key[T ~int32]() KeyCodec[T] { | ||
func (i int64Key[T]) WithName(name string) NamedKeyCodec[T] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve the undefined type The type Also applies to: 138-138 Toolsgolangci-lint
|
||
i.name = name | ||
return i | ||
} | ||
|
||
func (i int64Key[T]) Name() string { | ||
return i.name | ||
} | ||
|
||
func NewInt32Key[T ~int32]() NameableKeyCodec[T] { | ||
return int32Key[T]{} | ||
} | ||
|
||
type int32Key[T ~int32] struct{} | ||
type int32Key[T ~int32] struct { | ||
name string | ||
} | ||
|
||
func (i int32Key[T]) Encode(buffer []byte, key T) (int, error) { | ||
binary.BigEndian.PutUint32(buffer, (uint32)(key)) | ||
|
@@ -121,3 +134,12 @@ func (i int32Key[T]) DecodeNonTerminal(buffer []byte) (int, T, error) { | |
func (i int32Key[T]) SizeNonTerminal(_ T) int { | ||
return 4 | ||
} | ||
|
||
func (i int32Key[T]) WithName(name string) NamedKeyCodec[T] { | ||
i.name = name | ||
return i | ||
} | ||
|
||
func (i int32Key[T]) Name() string { | ||
return i.name | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package codec | ||
|
||
// NameableKeyCodec is a KeyCodec that can be named. | ||
type NameableKeyCodec[T any] interface { | ||
KeyCodec[T] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined interface Please ensure that Also applies to: 13-13 Toolsgolangci-lint
|
||
|
||
// WithName returns the KeyCodec with the provided name. | ||
WithName(name string) NamedKeyCodec[T] | ||
} | ||
|
||
// NamedKeyCodec is a KeyCodec that has a name. | ||
type NamedKeyCodec[T any] interface { | ||
KeyCodec[T] | ||
|
||
// Name returns the name of key in the schema if one is defined or the empty string. | ||
// Multipart keys should separate names with commas, i.e. "name1,name2". | ||
Name() string | ||
} | ||
|
||
// NameableValueCodec is a ValueCodec that can be named. | ||
type NameableValueCodec[T any] interface { | ||
ValueCodec[T] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined interface Please ensure that Also applies to: 30-30 Toolsgolangci-lint
|
||
|
||
// WithName returns the ValueCodec with the provided name. | ||
WithName(name string) NamedValueCodec[T] | ||
} | ||
|
||
// NamedValueCodec is a ValueCodec that has a name. | ||
type NamedValueCodec[T any] interface { | ||
ValueCodec[T] | ||
|
||
// Name returns the name of key in the schema if one is defined or the empty string. | ||
// Multipart keys should separate names with commas, i.e. "name1,name2". | ||
Name() string | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,16 @@ import ( | |
"fmt" | ||
) | ||
|
||
func NewStringKeyCodec[T ~string]() KeyCodec[T] { return stringKey[T]{} } | ||
func NewStringKeyCodec[T ~string]() NameableKeyCodec[T] { return stringKey[T]{} } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined interfaces Please ensure that Also applies to: 72-72 Toolsgolangci-lint
|
||
|
||
const ( | ||
// StringDelimiter defines the delimiter of a string key when used in non-terminal encodings. | ||
StringDelimiter uint8 = 0x0 | ||
) | ||
|
||
type stringKey[T ~string] struct{} | ||
type stringKey[T ~string] struct { | ||
name string | ||
} | ||
|
||
func (stringKey[T]) Encode(buffer []byte, key T) (int, error) { | ||
return copy(buffer, key), nil | ||
|
@@ -66,3 +68,12 @@ func (stringKey[T]) Stringify(key T) string { | |
func (stringKey[T]) KeyType() string { | ||
return "string" | ||
} | ||
|
||
func (s stringKey[T]) WithName(name string) NamedKeyCodec[T] { | ||
s.name = name | ||
return s | ||
} | ||
|
||
func (s stringKey[T]) Name() string { | ||
return s.name | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,11 @@ import ( | |
"strconv" | ||
) | ||
|
||
func NewUint64Key[T ~uint64]() KeyCodec[T] { return uint64Key[T]{} } | ||
func NewUint64Key[T ~uint64]() NameableKeyCodec[T] { return uint64Key[T]{} } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve the undefined type The type Also applies to: 69-69, 120-120 Toolsgolangci-lint
|
||
|
||
type uint64Key[T ~uint64] struct{} | ||
type uint64Key[T ~uint64] struct { | ||
name string | ||
} | ||
aaronc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func (uint64Key[T]) Encode(buffer []byte, key T) (int, error) { | ||
binary.BigEndian.PutUint64(buffer, (uint64)(key)) | ||
|
@@ -55,9 +57,20 @@ func (uint64Key[T]) KeyType() string { | |
return "uint64" | ||
} | ||
|
||
func NewUint32Key[T ~uint32]() KeyCodec[T] { return uint32Key[T]{} } | ||
func (u uint64Key[T]) WithName(name string) NamedKeyCodec[T] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve the undefined type The type Also applies to: 111-111, 162-162 Toolsgolangci-lint
|
||
u.name = name | ||
return u | ||
} | ||
|
||
func (u uint64Key[T]) Name() string { | ||
return u.name | ||
} | ||
|
||
func NewUint32Key[T ~uint32]() NameableKeyCodec[T] { return uint32Key[T]{} } | ||
|
||
type uint32Key[T ~uint32] struct{} | ||
type uint32Key[T ~uint32] struct { | ||
name string | ||
} | ||
|
||
func (uint32Key[T]) Encode(buffer []byte, key T) (int, error) { | ||
binary.BigEndian.PutUint32(buffer, (uint32)(key)) | ||
|
@@ -95,9 +108,20 @@ func (u uint32Key[T]) DecodeNonTerminal(buffer []byte) (int, T, error) { return | |
|
||
func (uint32Key[T]) SizeNonTerminal(_ T) int { return 4 } | ||
|
||
func NewUint16Key[T ~uint16]() KeyCodec[T] { return uint16Key[T]{} } | ||
func (u uint32Key[T]) WithName(name string) NamedKeyCodec[T] { | ||
u.name = name | ||
return u | ||
} | ||
|
||
type uint16Key[T ~uint16] struct{} | ||
func (u uint32Key[T]) Name() string { | ||
return u.name | ||
} | ||
|
||
func NewUint16Key[T ~uint16]() NameableKeyCodec[T] { return uint16Key[T]{} } | ||
|
||
type uint16Key[T ~uint16] struct { | ||
name string | ||
} | ||
|
||
func (uint16Key[T]) Encode(buffer []byte, key T) (int, error) { | ||
binary.BigEndian.PutUint16(buffer, (uint16)(key)) | ||
|
@@ -135,6 +159,15 @@ func (u uint16Key[T]) DecodeNonTerminal(buffer []byte) (int, T, error) { return | |
|
||
func (u uint16Key[T]) SizeNonTerminal(key T) int { return u.Size(key) } | ||
|
||
func (u uint16Key[T]) WithName(name string) NamedKeyCodec[T] { | ||
u.name = name | ||
return u | ||
} | ||
|
||
func (u uint16Key[T]) Name() string { | ||
return u.name | ||
} | ||
|
||
func uintEncodeJSON(value uint64) ([]byte, error) { | ||
str := `"` + strconv.FormatUint(value, 10) + `"` | ||
return []byte(str), nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package collections | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNaming(t *testing.T) { | ||
require.Equal(t, "u16", Uint16Key.WithName("u16").Name()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined variables used in tests. Please ensure that all keys and values ( Also applies to: 11-11, 12-12, 13-13, 14-14, 15-15, 16-16, 17-17, 19-19, 20-20, 21-21, 22-22, 23-23, 24-24, 25-25, 26-26, 28-28, 29-29 Toolsgolangci-lint
|
||
require.Equal(t, "u32", Uint32Key.WithName("u32").Name()) | ||
require.Equal(t, "u64", Uint64Key.WithName("u64").Name()) | ||
require.Equal(t, "i32", Int32Key.WithName("i32").Name()) | ||
require.Equal(t, "i64", Int64Key.WithName("i64").Name()) | ||
require.Equal(t, "str", StringKey.WithName("str").Name()) | ||
require.Equal(t, "bytes", BytesKey.WithName("bytes").Name()) | ||
require.Equal(t, "bool", BoolKey.WithName("bool").Name()) | ||
|
||
require.Equal(t, "vu16", Uint16Value.WithName("vu16").Name()) | ||
require.Equal(t, "vu32", Uint32Value.WithName("vu32").Name()) | ||
require.Equal(t, "vu64", Uint64Value.WithName("vu64").Name()) | ||
require.Equal(t, "vi32", Int32Value.WithName("vi32").Name()) | ||
require.Equal(t, "vi64", Int64Value.WithName("vi64").Name()) | ||
require.Equal(t, "vstr", StringValue.WithName("vstr").Name()) | ||
require.Equal(t, "vbytes", BytesValue.WithName("vbytes").Name()) | ||
require.Equal(t, "vbool", BoolValue.WithName("vbool").Name()) | ||
|
||
require.Equal(t, "abc,def", NamedPairKeyCodec[bool, string]("abc", BoolKey, "def", StringKey).Name()) | ||
require.Equal(t, "abc,def,ghi", NamedTripleKeyCodec[bool, string, int32]("abc", BoolKey, "def", StringKey, "ghi", Int32Key).Name()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be marked as API breaking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it API breaking?