Expression language(EL) to manipulate Golang structure data. Its main purpose is to find reflect.Value
by Expression, then do some reading and writing.
Simple as it takes to type the following command:
go get github.com/lysu/go-el
and import with
import github.com/lysu/go-el
As example, we have some data like this:
type Comment struct {
NickName string
Content string
Date time.Time
}
type Author struct {
Name string
}
type Blog struct {
Title string
RoleState map[string]uint
CommentIds []uint64
Comments map[string]*Comment
}
func (b Blog) FirstComment() *Comment {
return b.Comments["0"]
}
then we init them with some test data:
b := &Blog{
Title: "Blog title1",
CommentIds: []uint64{1, 3},
Comments: map[string]*Comment{
"0": {
NickName: "000",
Content: "test",
Date: time.Now(),
},
"1": {
NickName: "u1",
Content: "test",
Date: time.Now(),
},
"3": {
NickName: "tester",
Content: "test hehe...",
Date: time.Now(),
},
},
Author: Author{
Name: "Author 1",
},
RoleState: map[string]uint{},
}
Using el.Expression
, we can navigate from root(b
) to anywhere in this structure.
exp := el.Expression("Title")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> Blog title1
exp := el.Expression("Author.Name")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> Author 1
exp := el.Expression("CommentIds[0]")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> 1
exp := el.Expression("Comments["3"].NickName")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> tester
exp := el.Expression("Comments["CommentIds[0]].NickName")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> u1
function can return only ONE
result
exp := el.Expression("FirstComment().Content")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> test
After Execute
expression, we got a relfect.Value
, we also can use it to modify data, e.g.
exp := el.Expression("FirstComment().Content")
v, _ := exp.Execute(&data)
v.SetString("1111")
will let first comment with value 1111
Beside that we recommend users take a moment to look The Laws of Reflection, take care some limition that reflect has.
Base on Expression, we also provide a tool named Patcher
, the purpose of it is to let use modify object with expression easier and be batched.
We found it's very useful to build HTTP Patch API to partial update entity
ps := p.Patch{
"Author.Name": "ほん",
"Comments[CommentIds[0]].NickName": "私",
"roleState[100]": uint(100),
}
err := patcher.PatchIt(b, ps)
This will modify three properties at once~ (but we still meet some rule of refect, like map-value use ptr.. and so on)
See our Example in Unit-Test:
generate expression between two data..like diff..- -?
- Many code was extract from flosch/pongo2 --- An cool template-engine
- The presentation by Rob Pike titled Lexical Scanning in Go