Skip to content

Commit

Permalink
minor code cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
mitranim committed Oct 26, 2021
1 parent a159f12 commit ce1ccbd
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 30 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Arbitrarily composable and nestable structures.
* Uses data literals, not a builder API.
* Supports an optional "JSON Expression Language" (JEL) for expressing SQL expressions with nested Lisp-style calls in JSON.
* Supports safely parsing "order by" clauses from JSON and text, for specific struct types, converting field names from `"json"` field tags to `"db"` field tags.
* Decently optimized.
* Small and dependency-free.

Expand Down
2 changes: 1 addition & 1 deletion sqlb_bui.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (self Bui) Get() ([]byte, []interface{}) {
/*
Replaces text and args with the inputs. The following idiom is equivalent to
`bui.Expr` but more efficient if the expression type is concrete, avoiding an
interface allocation:
interface-induced allocation:
bui.Set(SomeExpr{}.AppendExpr(bui.Get()))
*/
Expand Down
2 changes: 1 addition & 1 deletion sqlb_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (self StructDict) GotNamed(key string) (interface{}, bool) {
access performance by about 3 times in our benchmarks.
*/

val := deref(self[0])
val := valueDeref(self[0])
if !val.IsValid() {
return nil, false
}
Expand Down
8 changes: 4 additions & 4 deletions sqlb_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func (self Seq) Append(text []byte) []byte { return exprAppend(&self, text) }
func (self Seq) String() string { return exprString(&self) }

func (self *Seq) any(bui *Bui, val interface{}) {
rval := elemValOf(val)
rval := valueOf(val)

switch rval.Kind() {
case reflect.Invalid:
Expand Down Expand Up @@ -833,7 +833,7 @@ func (self Cond) Append(text []byte) []byte { return exprAppend(&self, text) }
func (self Cond) String() string { return exprString(&self) }

func (self *Cond) any(bui *Bui, val interface{}) {
rval := elemValOf(val)
rval := valueOf(val)

switch rval.Kind() {
case reflect.Invalid:
Expand Down Expand Up @@ -946,7 +946,7 @@ type StructValues [1]interface{}
func (self StructValues) AppendExpr(text []byte, args []interface{}) ([]byte, []interface{}) {
bui := Bui{text, args}

val := elemValOf(self[0])
val := valueOf(self[0])
if val.IsValid() {
for i, field := range loadStructDbFields(val.Type()) {
if i > 0 {
Expand Down Expand Up @@ -1008,7 +1008,7 @@ type StructAssign [1]interface{}
func (self StructAssign) AppendExpr(text []byte, args []interface{}) ([]byte, []interface{}) {
bui := Bui{text, args}

val := elemValOf(self[0])
val := valueOf(self[0])
if val.IsValid() {
for i, field := range loadStructDbFields(val.Type()) {
if i > 0 {
Expand Down
4 changes: 2 additions & 2 deletions sqlb_jel.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const (
Shortcut for instantiating `Jel` with the type of the given value. The input is
used only as a type carrier.
*/
func JelFor(typ interface{}) Jel { return Jel{Type: elemTypeOf(typ)} }
func JelFor(typ interface{}) Jel { return Jel{Type: typeElemOf(typ)} }

/*
Short for "JSON Expression Language". Provides support for expressing a
Expand Down Expand Up @@ -201,7 +201,7 @@ nop. The input is used only as a type carrier; its actual value is ignored.
*/
func (self *Jel) OrType(typ interface{}) {
if self.Type == nil {
self.Type = elemTypeOf(typ)
self.Type = typeElemOf(typ)
}
}

Expand Down
2 changes: 1 addition & 1 deletion sqlb_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func DictQ(text string, args map[string]interface{}) StrQ {

// Shortcut for `StrQ{text, StructDict{reflect.ValueOf(args)}}`.
func StructQ(text string, args interface{}) StrQ {
val := elemValOf(args)
val := valueOf(args)
if !val.IsValid() {
return StrQ{text, nil}
}
Expand Down
18 changes: 10 additions & 8 deletions sqlb_ord.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ type is consulted when decoding orderings from an input such as JSON.
*/
func (self *OrdsParser) OrType(typ interface{}) {
if self.Type == nil {
self.Type = elemTypeOf(typ)
self.Type = typeElemOf(typ)
}
}

Expand Down Expand Up @@ -313,20 +313,22 @@ type Ord struct {

// Implement the `Expr` interface, making this a sub-expression.
func (self Ord) AppendExpr(text []byte, args []interface{}) ([]byte, []interface{}) {
return self.Append(text), args
}

// Implement the `Appender` interface, sometimes allowing more efficient text
// encoding.
func (self Ord) Append(text []byte) []byte {
if len(self.Path) > 0 {
text, args = self.Path.AppendExpr(text, args)
text = self.Path.Append(text)
text = self.Dir.Append(text)
text = self.Nulls.Append(text)
}
return text, args
return text
}

// Implement the `Appender` interface, sometimes allowing more efficient text
// encoding.
func (self Ord) Append(text []byte) []byte { return exprAppend(&self, text) }

// Implement the `fmt.Stringer` interface for debug purposes.
func (self Ord) String() string { return exprString(&self) }
func (self Ord) String() string { return appenderToStr(&self) }

// True if the path is empty.
func (self Ord) IsEmpty() bool { return len(self.Path) == 0 }
Expand Down
22 changes: 11 additions & 11 deletions sqlb_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ func typeElem(typ reflect.Type) reflect.Type {
return typ
}

func deref(val reflect.Value) reflect.Value {
func valueDeref(val reflect.Value) reflect.Value {
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return reflect.Value{}
Expand All @@ -586,16 +586,16 @@ func deref(val reflect.Value) reflect.Value {
return val
}

func elemTypeOf(typ interface{}) reflect.Type {
func typeElemOf(typ interface{}) reflect.Type {
return typeElem(reflect.TypeOf(typ))
}

func typeOf(typ interface{}) reflect.Type {
return typeDeref(reflect.TypeOf(typ))
}

func elemValOf(val interface{}) reflect.Value {
return deref(reflect.ValueOf(val))
func valueOf(val interface{}) reflect.Value {
return valueDeref(reflect.ValueOf(val))
}

func isStructEmpty(val interface{}) bool {
Expand Down Expand Up @@ -646,11 +646,11 @@ func typeName(typ reflect.Type) string {
}

func isNil(val interface{}) bool {
return val == nil || isValNil(reflect.ValueOf(val))
return val == nil || isValueNil(reflect.ValueOf(val))
}

func isValNil(rval reflect.Value) bool {
return !rval.IsValid() || isNilable(rval.Kind()) && rval.IsNil()
func isValueNil(val reflect.Value) bool {
return !val.IsValid() || isNilable(val.Kind()) && val.IsNil()
}

func isNilable(kind reflect.Kind) bool {
Expand All @@ -664,11 +664,11 @@ func isNilable(kind reflect.Kind) bool {

func isPublic(pkgPath string) bool { return pkgPath == `` }

func typeDeref(rtype reflect.Type) reflect.Type {
for rtype != nil && rtype.Kind() == reflect.Ptr {
rtype = rtype.Elem()
func typeDeref(typ reflect.Type) reflect.Type {
for typ != nil && typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
return rtype
return typ
}

/*
Expand Down
2 changes: 1 addition & 1 deletion t_jel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_Jel_Transcode(t *testing.T) {
]
`

expr := Jel{elemTypeOf((*External)(nil)), src}
expr := Jel{typeElemOf((*External)(nil)), src}
text, args := Reify(expr)

eq(
Expand Down
2 changes: 1 addition & 1 deletion t_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ func Test_Cols(t *testing.T) {
func Test_ColsDeep(t *testing.T) {
test := func(exp string, typ interface{}) {
t.Helper()
eq(t, exp, TypeColsDeep(elemTypeOf(typ)))
eq(t, exp, TypeColsDeep(typeElemOf(typ)))
testExpr(t, rei(exp), ColsDeep{typ})
}

Expand Down

0 comments on commit ce1ccbd

Please sign in to comment.