Coder Social home page Coder Social logo

jdolitsky / goluamapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yuin/gluamapper

0.0 1.0 0.0 42 KB

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

License: MIT License

Go 96.27% Makefile 3.73%
lua golang go parser mapper unmarshall golua

goluamapper's Introduction

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

goluamapper's People

Contributors

jdolitsky avatar yuin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.