/gluamapper

gluamapper: maps a GopherLua value to a Go value

Primary LanguageGoMIT LicenseMIT

gluamapper: maps a GopherLua value to a Go value

Build Status codecov PkgGoDev

gluamapper provides an easy way to map GopherLua values to Go values.

Installation

go get github.com/jinq0123/gluamapper

API

See Go doc.

Usage

    type Role struct {
        Name string
    }

    type Person struct {
        Name      string
        Age       int
        WorkPlace string
        Role      []*Role
    }

    L := lua.NewState()
    if err := L.DoString(`
      person = {
        Name = "Michel",
        Age  = 31,
        WorkPlace = "San Jose",
        Role = {
          {
            Name = "Administrator"
          },
          {
            Name = "Operator"
          }
        }
      }
    `); err != nil {
        panic(err)
    }
    var person Person
    if err := gluamapper.Map(L.GetGlobal("person"), &person); err != nil {
        panic(err)
    }
    fmt.Printf("%s %d", person.Name, person.Age)

License

MIT

Author

  • Yusuke Inuzuka
  • Jin Qing

Differences from yuin/gluamapper

  • Speedup

    • Converts directly from Lua table to Go struct, while yuin/gluamapper converts the table to map[string]interface{}, and then converts it to a Go struct using mapstructure.
    • No "weak" conversions
      • returns error if types are different
      • only convert Lua number to int types
    • Always ignores unused keys
  • New feature

    • Maps Lua types other than table to Go types
    • Maps Lua user data to Go value
  • Bugfix

    • TODO: circular reference

TODO

  • handle embedded fields