gluamapper

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2023 License: MIT Imports: 6 Imported by: 1

README

# Gluamapper

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Id

func Id(s string) string

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

func Map

func Map(tbl *lua.LTable, st interface{}) 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(nil)
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 ToGoValue

func ToGoValue(lv lua.LValue, opt Option) interface{}

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 interface{}) error

Map maps the lua table to the given struct pointer.

type Option

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

	// Returns error if unused keys exist.
	ErrorUnused bool

	// A struct tag name for lua table keys . This defaults to "gluamapper"
	TagName string
}

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