-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from olachat/yinloo/refactor-column-interface
split interface. add params helper and ColumnVal
- Loading branch information
Showing
7 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package coredb | ||
|
||
type ColumnVal[T any] struct { | ||
val T | ||
} | ||
|
||
func (c *ColumnVal[T]) GetValPointer() any { | ||
return &c.val | ||
} | ||
|
||
func (c *ColumnVal[T]) GetVal() T { | ||
return c.val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package coredb | ||
|
||
// ColumnType defines the generated type of a table column | ||
type ColumnType interface { | ||
GetColumnName() string | ||
type ColumnValPointer interface { | ||
GetValPointer() any | ||
} | ||
type ColumnNamer interface { | ||
GetColumnName() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package coredb | ||
|
||
import "reflect" | ||
|
||
type Params struct { | ||
params []any | ||
} | ||
|
||
func NewParams(params ...any) *Params { | ||
return &Params{params} | ||
} | ||
|
||
func (p *Params) Add(params ...any) { | ||
totalLen := len(p.params) | ||
for i := 0; i < len(params); i++ { | ||
if v := reflect.ValueOf(params[i]); v.Kind() == reflect.Slice { | ||
totalLen += v.Len() | ||
} else { | ||
totalLen++ | ||
} | ||
} | ||
|
||
newSlice := make([]any, 0, totalLen) | ||
newSlice = append(newSlice, p.params...) | ||
for i := 0; i < len(params); i++ { | ||
if v := reflect.ValueOf(params[i]); v.Kind() == reflect.Slice { | ||
// append all elements in params[0] to p.params | ||
for j := 0; j < v.Len(); j++ { | ||
newSlice = append(newSlice, v.Index(j).Interface()) | ||
} | ||
} else { | ||
newSlice = append(newSlice, params[i]) | ||
} | ||
} | ||
p.params = newSlice | ||
} | ||
|
||
func (p *Params) Get() []any { | ||
return p.params | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package coredb | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewParams(t *testing.T) { | ||
as := assert.New(t) | ||
p := NewParams(1, "1", 1.0) | ||
out := p.Get() | ||
as.Equal(1, out[0]) | ||
as.Equal("1", out[1]) | ||
as.Equal(1.0, out[2]) | ||
|
||
p.Add([]string{"2", "two"}, "king", -66) | ||
out = p.Get() | ||
as.Equal(1, out[0]) | ||
as.Equal("1", out[1]) | ||
as.Equal(1.0, out[2]) | ||
as.Equal("2", out[3]) | ||
as.Equal("two", out[4]) | ||
as.Equal("king", out[5]) | ||
as.Equal(-66, out[6]) | ||
|
||
p.Add(0.6, 77, []int{88, 99, 100}) | ||
out = p.Get() | ||
as.Equal(1, out[0]) | ||
as.Equal("1", out[1]) | ||
as.Equal(1.0, out[2]) | ||
as.Equal("2", out[3]) | ||
as.Equal("two", out[4]) | ||
as.Equal("king", out[5]) | ||
as.Equal(-66, out[6]) | ||
as.Equal(0.6, out[7]) | ||
as.Equal(77, out[8]) | ||
as.Equal(88, out[9]) | ||
as.Equal(99, out[10]) | ||
as.Equal(100, out[11]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters