Coder Social home page Coder Social logo

go-lookup's Introduction

go-lookup Build Status GoDoc

Small library on top of reflect for make lookups to Structs or Maps. Using a very simple DSL you can access to any property, key or value of any value of Go.

Installation

The recommended way to install go-lookup

go get github.com/mcuadros/go-lookup

Example

type Cast struct {
  Actor, Role string
}

type Serie struct {
  Cast []Cast
}

series := map[string]Serie{
  "A-Team": {Cast: []Cast{
    {Actor: "George Peppard", Role: "Hannibal"},
    {Actor: "Dwight Schultz", Role: "Murdock"},
    {Actor: "Mr. T", Role: "Baracus"},
    {Actor: "Dirk Benedict", Role: "Faceman"},
  }},
}

q := "A-Team.Cast.Role"
value, _ := LookupString(series, q)
fmt.Println(q, "->", value.Interface())
// A-Team.Cast.Role -> [Hannibal Murdock Baracus Faceman]

q = "A-Team.Cast[0].Actor"
value, _ = LookupString(series, q)
fmt.Println(q, "->", value.Interface())
// A-Team.Cast[0].Actor -> George Peppard

Case-insensitive matching

Use the LookupI and LookupStringI functions to do a case-insensitive match on struct field names and map keys. It will first look for an exact match; if that fails, it will fall back to a more expensive linear search over fields/keys.

type ExampleStruct struct {
  SoftwareUpdated bool
}

i := ExampleStruct{
  SoftwareUpdated: true,
}

value, _ := LookupStringI(i, "softwareupdated")
fmt.Println(value.Interface())
// Output: true

License

MIT, see LICENSE

go-lookup's People

Contributors

dansimau avatar erizocosmico avatar jirikuncar avatar mcuadros avatar tcard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-lookup's Issues

Lookup slice index of map field?

I was expecting this to work but it does not. I'm using commit 5415b5b

func TestLookupMapSliceIndex(t *testing.T) {
	assert := assert.New(t)
	m := map[string]any{
		"test-name": []string{"test-value"},
	}

	v, e := lookup.LookupString(m, "test-name[0]")
	assert.NoError(e)
	assert.Equal("test-value", v.String())
}

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.