gluamapper

package
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2025 License: Apache-2.0, MIT Imports: 8 Imported by: 0

README

===============================================================================
gluamapper: maps a GopherLua table to a Go struct
===============================================================================

.. image:: https://godoc.org/github.com/yuin/gluamapper?status.svg
    :target: http://godoc.org/github.com/yuin/gluamapper

.. image:: https://travis-ci.org/yuin/gluamapper.svg
    :target: https://travis-ci.org/yuin/gluamapper

|

gluamapper provides an easy way to map GopherLua tables to Go structs.

gluamapper converts a GopherLua table to ``map[string]interface{}`` , and then converts it to a Go struct using `mapstructure <https://github.com/mitchellh/mapstructure/>`_ .
 
----------------------------------------------------------------
Installation
----------------------------------------------------------------

.. code-block:: bash
   
   go get github.com/yuin/gluamapper

----------------------------------------------------------------
API
----------------------------------------------------------------
See `Go doc <http://godoc.org/github.com/yuin/gluamapper>`_ .

----------------------------------------------------------------
Usage
----------------------------------------------------------------

.. code-block:: go

    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", -- weakly input
      work_place = "San Jose",
      role = {
        {
          name = "Administrator"
        },
        {
          name = "Operator"
        }
      }
    }
    `); err != nil {
        panic(err)
    }
    var person Person
    if err := gluamapper.Map(L.GetGlobal("person").(*lua.LTable), &person); err != nil {
        panic(err)
    }
    fmt.Printf("%s %d", person.Name, person.Age)

----------------------------------------------------------------
License
----------------------------------------------------------------
MIT

----------------------------------------------------------------
Author
----------------------------------------------------------------
Yusuke Inuzuka

Documentation

Overview

gluamapper provides an easy way to map GopherLua tables to Go structs.

Index

Examples

Constants

View Source
const StructTagName = "gluamapper"

Variables

This section is empty.

Functions

func FromGoValue

func FromGoValue(L *lua.LState, v any) lua.LValue

func Identity

func Identity(s string) string

Identity is an Option.NameFunc that returns given string as-is.

func Map

func Map(tbl *lua.LTable, st any) error

Map maps the lua table to the given struct pointer with default options.

Example
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", -- weakly input
      work_place = "San Jose",
      role = {
        {
          name = "Administrator"
        },
        {
          name = "Operator"
        }
      }
    }
	`); err != nil {
	panic(err)
}
var person Person
if err := Map(L.GetGlobal("person").(*lua.LTable), &person); err != nil {
	panic(err)
}
fmt.Printf("%s %d", person.Name, person.Age)
Output:
Michel 31

func MatchSnakeCase

func MatchSnakeCase(mapKey, fieldName string) bool

func ToGoValue

func ToGoValue(lv lua.LValue, opt Option) any

ToGoValue converts the given LValue to a Go object.

func ToUpperCamelCase

func ToUpperCamelCase(s string) string

ToUpperCamelCase is an Option.NameFunc that converts strings from snake case to upper camel case.

Types

type Mapper

type Mapper struct {
	Option Option
}

Mapper maps a lua table to a Go struct pointer.

func NewMapper

func NewMapper(opt Option) *Mapper

NewMapper returns a new mapper.

func (*Mapper) Map

func (mapper *Mapper) Map(tbl *lua.LTable, st any) error

Map maps the lua table to the given struct pointer.

func (*Mapper) ToGoValue

func (mapper *Mapper) ToGoValue(lv lua.LValue) any

type Option

type Option struct {
	// Function to convert a lua table key to Go's one. This defaults to "Id".
	NameFunc func(string) string

	DecoderConfig *mapstructure.DecoderConfig
}

Option is a configuration that is used to create a new mapper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL