luhelp

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 13 Imported by: 0

README

luhelp

This is the lua helper layer that exposes functions to

  • Map go values to lua ones
  • Map lua values to go ones
  • Bind lua functions to go functions

Naming Conventions

To conform to the naming conventions of both languages, go structs and map keys are converted to snake_case and lua table keys will be converted to CamelCase when passing data.

Documentation

Index

Constants

View Source
const LuaTag = "lua"

Variables

This section is empty.

Functions

func ToGoValue

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

ToGoValue converts the given LValue to a Go object.

func ToLua

func ToLua(state *lua.LState, val any) lua.LValue

ToLua converts a go value to a lua value.

func ToString

func ToString(val lua.LValue, mapper *Mapper) string

ToString converts a lua value to a string.

Types

type Mapper

type Mapper struct {
	Option Option
}

Mapper maps a lua table to a Go struct pointer.

func NewMapper

func NewMapper(state *lua.LState) *Mapper

NewMapper returns a new mapper bound to a lua state.

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 {
	// NameFunc to convert a lua table key to Go's one. This defaults to "ToUpperCamelCase".
	NameFunc func(string) string

	// ErrorUnused returns error if unused keys exist.
	ErrorUnused bool

	// TagName struct tag name for lua table keys.
	TagName string

	// DecodeHook for MapStructure.
	DecodeHook any

	// FnHook to convert lua function to some go value.
	FnHook func(value lua.LValue) any
}

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

type OwnedCallback

type OwnedCallback func(args ...any) (any, error)

OwnedCallback represents a callback that will execute inside a lua vm.

func BindToLua

func BindToLua(state *lua.LState, value lua.LValue) OwnedCallback

BindToLua will create a OwnedCallback from a lua function and state.

func (OwnedCallback) Call

func (cb OwnedCallback) Call(args ...any) (any, error)

Call executes the callback with the given arguments. If the callback is nil it will return nil, nil. If the callback returns an error it will be returned as the error of this function.

func (OwnedCallback) MarshalJSON

func (cb OwnedCallback) MarshalJSON() ([]byte, error)

func (OwnedCallback) Present

func (cb OwnedCallback) Present() bool

Present returns true if the callback is not nil.

Jump to

Keyboard shortcuts

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