-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
232 additions
and
16 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
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
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,88 @@ | ||
package sqlnt | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
const sqlTag = "sql" | ||
|
||
// NewTemplateSet builds a set of templates for the given struct type T | ||
// | ||
// Fields of type sqlnt.NamedTemplate are created and set from the field tag 'sql' | ||
// | ||
// Example: | ||
// | ||
// type MyTemplateSet struct { | ||
// Select sqlnt.NamedTemplate `sql:"SELECT * FROM foo WHERE col_a = :a"` | ||
// Insert sqlnt.NamedTemplate `sql:"INSERT INTO foo (col_a, col_b, col_c) VALUES(:a, :b, :c)"` | ||
// Delete sqlnt.NamedTemplate `sql:"DELETE FROM foo WHERE col_a = :a"` | ||
// } | ||
// set, err := sqlnt.NewTemplateSet[MyTemplateSet]() | ||
// | ||
// Note: If the overall field tag does not contain a 'sql' tag nor any other tags (i.e. there are no double-quotes in it) | ||
// then the entire field tag value is used as the template - enabling the use of carriage returns to format the statement | ||
// | ||
// Example: | ||
// | ||
// type MyTemplateSet struct { | ||
// Select sqlnt.NamedTemplate `SELECT * | ||
// FROM foo | ||
// WHERE col_a = :a` | ||
// } | ||
func NewTemplateSet[T any](options ...any) (*T, error) { | ||
var chk T | ||
if reflect.TypeOf(chk).Kind() != reflect.Struct { | ||
return nil, errors.New("not a struct") | ||
} | ||
r := new(T) | ||
if err := setTemplateFields(reflect.ValueOf(r).Elem(), options...); err != nil { | ||
return nil, err | ||
} | ||
return r, nil | ||
} | ||
|
||
// MustCreateTemplateSet is the same as NewTemplateSet except that it panics on error | ||
func MustCreateTemplateSet[T any](options ...any) *T { | ||
r, err := NewTemplateSet[T](options...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return r | ||
} | ||
|
||
var ntt = reflect.TypeOf((*NamedTemplate)(nil)).Elem() | ||
|
||
func setTemplateFields(rv reflect.Value, options ...any) error { | ||
rvt := rv.Type() | ||
for i := 0; i < rv.NumField(); i++ { | ||
fld := rv.Field(i) | ||
if ft := rvt.Field(i); ft.IsExported() { | ||
if fld.Type() == ntt { | ||
tag, ok := ft.Tag.Lookup(sqlTag) | ||
if !ok { | ||
if ft.Tag != "" && !strings.ContainsRune(string(ft.Tag), '"') { | ||
tag = string(ft.Tag) | ||
} else { | ||
return fmt.Errorf("field '%s' does not have '%s' tag", ft.Name, sqlTag) | ||
} | ||
} | ||
if tmp, err := NewNamedTemplate(tag, options...); err == nil { | ||
fld.Set(reflect.ValueOf(tmp)) | ||
} else { | ||
return err | ||
} | ||
} else if fld.Kind() == reflect.Struct { | ||
sub := reflect.New(fld.Type()).Elem() | ||
if err := setTemplateFields(sub, options...); err == nil { | ||
fld.Set(sub) | ||
} else { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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,105 @@ | ||
package sqlnt | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
type MySet struct { | ||
Select NamedTemplate `SELECT * | ||
FROM {{tableName}} | ||
WHERE col_a = :a` | ||
Insert NamedTemplate `sql:"INSERT INTO {{tableName}} (col_a,col_b,col_c) VALUES (:a,:b,:c)"` | ||
Delete NamedTemplate `sql:"DELETE FROM {{tableName}} WHERE col_a = :a"` | ||
delete NamedTemplate // unexported fields not used | ||
} | ||
|
||
type MySet2 struct { | ||
MySet | ||
} | ||
|
||
type MySet3 struct { | ||
MySet2 | ||
} | ||
|
||
func TestNewTemplateSet(t *testing.T) { | ||
ts, err := NewTemplateSet[MySet](testTokenOption) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, ts.Select) | ||
assert.Equal(t, "SELECT *\nFROM foo\nWHERE col_a = ?", ts.Select.Statement()) | ||
assert.NotNil(t, ts.Insert) | ||
assert.Equal(t, "INSERT INTO foo (col_a,col_b,col_c) VALUES (?,?,?)", ts.Insert.Statement()) | ||
assert.NotNil(t, ts.Delete) | ||
assert.Equal(t, "DELETE FROM foo WHERE col_a = ?", ts.Delete.Statement()) | ||
assert.Nil(t, ts.delete) | ||
} | ||
|
||
func TestNewTemplateSet_Anonymous(t *testing.T) { | ||
ts, err := NewTemplateSet[struct { | ||
Select NamedTemplate `SELECT * | ||
FROM {{tableName}} | ||
WHERE col_a = :a` | ||
}](testTokenOption) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, ts.Select) | ||
assert.Equal(t, "SELECT *\nFROM foo\nWHERE col_a = ?", ts.Select.Statement()) | ||
} | ||
|
||
func TestNewTemplateSet_Nested(t *testing.T) { | ||
ts, err := NewTemplateSet[MySet2](testTokenOption) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, ts.Select) | ||
} | ||
|
||
func TestNewTemplateSet_NestedDouble(t *testing.T) { | ||
ts, err := NewTemplateSet[MySet3](testTokenOption) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, ts.Select) | ||
} | ||
|
||
func TestNewTemplateSet_Error_NotStruct(t *testing.T) { | ||
_, err := NewTemplateSet[string](testTokenOption) | ||
assert.Error(t, err) | ||
assert.Equal(t, "not a struct", err.Error()) | ||
} | ||
|
||
type BadSet1 struct { | ||
Select NamedTemplate // doesn't have 'sql' tag | ||
} | ||
|
||
func TestNewTemplateSet_Error_NoSqlTag(t *testing.T) { | ||
_, err := NewTemplateSet[BadSet1](testTokenOption) | ||
assert.Error(t, err) | ||
assert.Equal(t, "field 'Select' does not have 'sql' tag", err.Error()) | ||
} | ||
|
||
type BadSet2 struct { | ||
Select NamedTemplate `sql:"{{unknown_token}}"` | ||
} | ||
|
||
func TestNewTemplateSet_Error_BadTemplate(t *testing.T) { | ||
_, err := NewTemplateSet[BadSet2](testTokenOption) | ||
assert.Error(t, err) | ||
assert.Equal(t, "unknown token: unknown_token", err.Error()) | ||
} | ||
|
||
type BadSet3 struct { | ||
BadSet2 | ||
} | ||
|
||
func TestNewTemplateSet_Error_NestedBadTemplate(t *testing.T) { | ||
_, err := NewTemplateSet[BadSet3](testTokenOption) | ||
assert.Error(t, err) | ||
assert.Equal(t, "unknown token: unknown_token", err.Error()) | ||
} | ||
|
||
func TestMustCreateTemplateSet(t *testing.T) { | ||
ts := MustCreateTemplateSet[MySet](testTokenOption) | ||
assert.NotNil(t, ts.Select) | ||
} | ||
|
||
func TestMustCreateTemplateSet_Panics(t *testing.T) { | ||
assert.Panics(t, func() { | ||
_ = MustCreateTemplateSet[BadSet1]() | ||
}) | ||
} |