Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Commit

Permalink
Make scanner replacable
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Turpitko committed May 24, 2017
1 parent bd7d241 commit c5475cf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 43 deletions.
35 changes: 19 additions & 16 deletions funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@ import (
"github.com/nikolay-turpitko/structor/funcs/strings"
"github.com/nikolay-turpitko/structor/funcs/use"
"github.com/nikolay-turpitko/structor/funcs/xpath"
"github.com/nikolay-turpitko/structor/scanner"
)

var testEvaluator = structor.NewEvaluator(structor.Interpreters{
structor.WholeTag: &el.DefaultInterpreter{
AutoEnclose: true,
Funcs: use.Packages(
use.Pkg{Prefix: "b_", Funcs: bytes.Pkg},
use.Pkg{Prefix: "c_", Funcs: crypt.Pkg},
use.Pkg{Prefix: "e_", Funcs: encoding.Pkg},
use.Pkg{Prefix: "g_", Funcs: goquery.Pkg},
use.Pkg{Prefix: "m_", Funcs: math.Pkg},
use.Pkg{Prefix: "o_", Funcs: funcs_os.Pkg},
use.Pkg{Prefix: "r_", Funcs: regexp.Pkg},
use.Pkg{Prefix: "s_", Funcs: strings.Pkg},
use.Pkg{Prefix: "x_", Funcs: xpath.Pkg},
),
},
})
var testEvaluator = structor.NewEvaluator(
scanner.Default,
structor.Interpreters{
structor.WholeTag: &el.DefaultInterpreter{
AutoEnclose: true,
Funcs: use.Packages(
use.Pkg{Prefix: "b_", Funcs: bytes.Pkg},
use.Pkg{Prefix: "c_", Funcs: crypt.Pkg},
use.Pkg{Prefix: "e_", Funcs: encoding.Pkg},
use.Pkg{Prefix: "g_", Funcs: goquery.Pkg},
use.Pkg{Prefix: "m_", Funcs: math.Pkg},
use.Pkg{Prefix: "o_", Funcs: funcs_os.Pkg},
use.Pkg{Prefix: "r_", Funcs: regexp.Pkg},
use.Pkg{Prefix: "s_", Funcs: strings.Pkg},
use.Pkg{Prefix: "x_", Funcs: xpath.Pkg},
),
},
})

func TestCrypt(t *testing.T) {
type theStruct struct {
Expand Down
17 changes: 11 additions & 6 deletions structor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ const WholeTag = ""
// processed using different EL interpreters.
//
// interpreters - is a map of registered tag names to EL interpreters.
func NewEvaluator(interpreters Interpreters) Evaluator {
func NewEvaluator(
scanner scanner.Scanner,
interpreters Interpreters) Evaluator {
if len(interpreters) == 0 {
panic("no interpreters registered")
}
return &evaluator{interpreters}
return &evaluator{scanner, interpreters}
}

// NewDefaultEvaluator returns default Evaluator implementation. Default
Expand All @@ -72,12 +74,15 @@ func NewEvaluator(interpreters Interpreters) Evaluator {
//
// funcs - custom functions, available for interpreter;
func NewDefaultEvaluator(funcs use.FuncMap) Evaluator {
return NewEvaluator(Interpreters{
"eval": &el.DefaultInterpreter{Funcs: funcs},
})
return NewEvaluator(
scanner.Default,
Interpreters{
"eval": &el.DefaultInterpreter{Funcs: funcs},
})
}

type evaluator struct {
scanner scanner.Scanner
interpreters Interpreters
}

Expand Down Expand Up @@ -167,7 +172,7 @@ func (ev evaluator) fieldIntrospect(
i int) (fieldDescr, error) {
f := typ.Field(i)
v := indirect(val.Field(i))
tags, err := scanner.Default.Tags(f.Tag)
tags, err := ev.scanner.Tags(f.Tag)
res := fieldDescr{
name: f.Name,
value: v,
Expand Down
51 changes: 30 additions & 21 deletions structor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/nikolay-turpitko/structor/funcs/math"
"github.com/nikolay-turpitko/structor/funcs/strings"
"github.com/nikolay-turpitko/structor/funcs/use"
"github.com/nikolay-turpitko/structor/scanner"
)

// TestSimple tests simple structor usage: string fields, data from context,
Expand Down Expand Up @@ -142,9 +143,11 @@ func (cc) Execute(expr string, _ *el.Context) (interface{}, error) {

// TestCustomInterpretor tests usage of custom interpreter and tag name.
func TestCustomInterpretor(t *testing.T) {
ev := structor.NewEvaluator(structor.Interpreters{
"cc": &cc{},
})
ev := structor.NewEvaluator(
scanner.Default,
structor.Interpreters{
"cc": &cc{},
})
type theStruct struct {
A int `cc:"something"`
}
Expand All @@ -157,9 +160,11 @@ func TestCustomInterpretor(t *testing.T) {
// TestWholeTag tests usage of the whole tag value as an expression for custom
// interpreter.
func TestWholeTag(t *testing.T) {
ev := structor.NewEvaluator(structor.Interpreters{
structor.WholeTag: &cc{},
})
ev := structor.NewEvaluator(
scanner.Default,
structor.Interpreters{
structor.WholeTag: &cc{},
})
type theStruct struct {
A int `this whole string should be processed as an EL expression`
}
Expand All @@ -172,12 +177,14 @@ func TestWholeTag(t *testing.T) {
// TestWholeTagAutoEnclose tests usage of the whole tag value as an
// text/template EL expression with automatic enclosing into delimiters.
func TestWholeTagAutoEnclose(t *testing.T) {
ev := structor.NewEvaluator(structor.Interpreters{
structor.WholeTag: &el.DefaultInterpreter{
AutoEnclose: true,
Funcs: math.Pkg,
},
})
ev := structor.NewEvaluator(
scanner.Default,
structor.Interpreters{
structor.WholeTag: &el.DefaultInterpreter{
AutoEnclose: true,
Funcs: math.Pkg,
},
})
type theStruct struct {
A int `set 40`
B int `set 2`
Expand All @@ -191,15 +198,17 @@ func TestWholeTagAutoEnclose(t *testing.T) {

// Example of usage structor.
func Example() {
ev := structor.NewEvaluator(structor.Interpreters{
structor.WholeTag: &el.DefaultInterpreter{
AutoEnclose: true,
Funcs: use.Packages(
use.Pkg{Funcs: math.Pkg},
use.Pkg{Funcs: strings.Pkg},
),
},
})
ev := structor.NewEvaluator(
scanner.Default,
structor.Interpreters{
structor.WholeTag: &el.DefaultInterpreter{
AutoEnclose: true,
Funcs: use.Packages(
use.Pkg{Funcs: math.Pkg},
use.Pkg{Funcs: strings.Pkg},
),
},
})
type theStruct struct {
A int `set 40`
B int `set 2`
Expand Down

0 comments on commit c5475cf

Please sign in to comment.