Skip to content

Commit

Permalink
refactor(proto): update enum8
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Dec 26, 2021
1 parent 710600c commit b8d7ac5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
17 changes: 11 additions & 6 deletions proto/col_enum8.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ import (
)

// ColEnum8Auto is inference helper for ColEnum8.
//
// You can set Values and actual enum mapping will be inferred during query
// execution.
type ColEnum8Auto struct {
t ColumnType

rawToStr map[Enum8]string
strToRaw map[string]Enum8
raw ColEnum8

Str []string
// Values of Enum8.
Values []string
}

// Append value to Enum8 column.
func (e *ColEnum8Auto) Append(v string) {
e.Str = append(e.Str, v)
e.Values = append(e.Values, v)
}

func (e *ColEnum8Auto) parse(t ColumnType) error {
Expand Down Expand Up @@ -69,7 +74,7 @@ func (e *ColEnum8Auto) Infer(t ColumnType) error {
}

func (e *ColEnum8Auto) Rows() int {
return len(e.Str)
return len(e.Values)
}

func (e *ColEnum8Auto) DecodeColumn(r *Reader, rows int) error {
Expand All @@ -81,18 +86,18 @@ func (e *ColEnum8Auto) DecodeColumn(r *Reader, rows int) error {
if !ok {
return errors.Errorf("unknown enum value %d", v)
}
e.Str = append(e.Str, s)
e.Values = append(e.Values, s)
}
return nil
}

func (e *ColEnum8Auto) Reset() {
e.raw.Reset()
e.Str = e.Str[:0]
e.Values = e.Values[:0]
}

func (e *ColEnum8Auto) Prepare() error {
for _, v := range e.Str {
for _, v := range e.Values {
raw, ok := e.strToRaw[v]
if !ok {
return errors.Errorf("unknown enum value %s", v)
Expand Down
6 changes: 3 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestClient_Query(t *testing.T) {
require.NoError(t, conn.Do(ctx, createTable), "create table")

data := proto.ColEnum8Auto{
Str: []string{"foo", "bar"},
Values: []string{"foo", "bar"},
}
insertQuery := Query{
Body: "INSERT INTO test_table VALUES",
Expand All @@ -106,8 +106,8 @@ func TestClient_Query(t *testing.T) {
},
}
require.NoError(t, conn.Do(ctx, selectData), "select")
require.Equal(t, data.Str, gotData.Str)
t.Log(gotData.Str)
require.Equal(t, data.Values, gotData.Values)
t.Log(gotData.Values)
})
t.Run("InsertTuple", func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit b8d7ac5

Please sign in to comment.