Coder Social home page Coder Social logo

go-el's Introduction

go-el

Expression language(EL) to manipulate Golang structure data. Its main purpose is to find reflect.Value by Expression, then do some reading and writing.

Installation

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    

Usage

Example Data

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{},
}

Expression

Using el.Expression, we can navigate from root(b) to anywhere in this structure.

1. To field

exp := el.Expression("Title")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> Blog title1

2. To nested field

exp := el.Expression("Author.Name")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> Author 1

3. To slice/array/string item

exp := el.Expression("CommentIds[0]")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> 1

4. To map item

exp := el.Expression("Comments["3"].NickName")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> tester

5. Item in[] also can be another Expression

exp := el.Expression("Comments["CommentIds[0]].NickName")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> u1

6. Call function

function can return only ONE result

exp := el.Expression("FirstComment().Content")
v, _ := exp.Execute(&data)
fmt.Printf("%v\n", v.interface()) //==> test  

7. Modify Value

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.

Patcher

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)

More

See our Example in Unit-Test:

TODO

generate expression between two data..like diff..- -?

Thanks

go-el's People

Contributors

lysu avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.