goluamapper

package module
v0.0.0-...-22d1c46 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2019 License: MIT Imports: 6 Imported by: 0

README

goluamapper: maps an Azure/golua table to a Go struct

image

goluamapper provides an easy way to map Azure/golua tables to Go structs.

goluamapper converts an Azure/golua table to map[string]interface{}, and then converts it to a Go struct using mapstructure.

API

See Go doc.

Usage

See the source for the example below.

This example will evaluate a Lua script (as string), extract the resulting person global variable, and map it to our custom Person type:

package main

import (
	"fmt"

	"github.com/Azure/golua/lua"
	"github.com/Azure/golua/std"
	"github.com/jdolitsky/goluamapper"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	state := lua.NewState()
	defer state.Close()
	std.Open(state)

	// Evaluate the Lua script.
	// We can also use state.ExecFile(<filepath>)
	err := state.ExecText(`
		local name = "Fred"
		local age = 42

		person = {
			name = name,
			age = age
		}
	`)
	if err != nil {
		panic(err)
	}

	// Extract the "person" global var and map to Person type
	var person Person
	state.GetGlobal("person")
	err = goluamapper.Map(state.Pop(), &person)
	if err != nil {
		panic(err)
	}

	// Should print "Fred 42"
	fmt.Printf("%s %d\n", person.Name, person.Age)
}

License

MIT

Original Author

Yusuke Inuzuka

Source: https://github.com/yuin/gluamapper

Documentation

Overview

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

Index

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(v lua.Value, st interface{}) error

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

func ToGoValue

func ToGoValue(v lua.Value, opt Option) reflect.Value

ToGoValue converts the given LValue to a Go object. adapted form https://github.com/Azure/golua/blob/master/pkg/luautil/reflect.go

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(v lua.Value, 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 "goluamapper"
	TagName string
}

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

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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