yaml

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: BSD-3-Clause Imports: 4 Imported by: 0

README

yaml GoDoc

Usage

decode
local yaml = require("yaml")
local inspect = require("inspect")

-- yaml.decode()
local text = [[
a:
  b: 1
]]
local result, err = yaml.decode(text)
if err then
    error(err)
end
print(inspect(result, { newline = "", indent = "" }))
-- Output:
-- {a = {b = 1}}
encode
    local yaml = require("yaml")
    local encoded, err = yaml.encode({ a = { b = 1 } })
    if err then
        error(err)
    end
    print(encoded)
    -- Output:
    -- a:
    --   b: 1
    --
decoder

Using a decoder allows reading from file or strings.Reader with input that has multiple values

  • With file
local yaml = require("yaml")
local io = require("io")
local inspect = require("inspect")

f, err = io.open("myfile.yaml", "r")
assert(not err, err)
decoder = yaml.new_decoder(f)
result, err = decoder:decode()
f:close()
assert(not err, err)
print(inspect(result))
  • With strings.Reader
local yaml = require("yaml")
local strings = require("strings")
local inspect = require("inspect")

reader = strings.new_reader([[
foo: bar
num: 123
arr:
- abc
- def
- ghi
]])
decoder = yaml.new_decoder(reader)
result, err = decoder:decode()
f:close()
assert(not err, err)
print(inspect(result))
encoder

Using an allows writing to file or strings.Builder and to write multiple values if desired

  • with file
local yaml = require("yaml")
local io = require("io")

f, err = io.open('myfile.yaml', 'w')
assert(not err, err)
encoder = yaml.new_encoder(f)
err = encoder:encode({ abc = "def", num = 123, arr = { 1, 2, 3 } })
assert(not err, err)
  • with strings.Builder
local yaml = require("yaml")
local strings = require("strings")

writer = strings.new_builder()
encoder = yaml.new_encoder(writer)
err = encoder:encode({ abc = "def", num = 123, arr = { 1, 2, 3 } })
assert(not err, err)
s = writer.string()
print(s)

Documentation

Overview

Package yaml implements yaml decode functionality for lua.

Example

yaml.decode(string)

state := lua.NewState()
Preload(state)
inspect.Preload(state)
source := `
    local yaml = require("yaml")
    local inspect = require("inspect")
    local text = [[
a:
  b: 1
    ]]
    local result, err = yaml.decode(text)
    if err then error(err) end
    print(inspect(result, {newline="", indent=""}))
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

{a = {b = 1}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckYAMLDecoder added in v0.1.6

func CheckYAMLDecoder(L *lua.LState, n int) *yaml.Decoder

func CheckYAMLEncoder added in v0.1.6

func CheckYAMLEncoder(L *lua.LState, n int) *yaml.Encoder

func Decode

func Decode(L *lua.LState) int

Decode lua yaml.decode(string) returns (table, error)

func Encode

func Encode(L *lua.LState) int

Encode lua yaml.encode(any) returns (string, error)

Example
state := lua.NewState()
Preload(state)
inspect.Preload(state)
source := `
    local yaml = require("yaml")
    local encoded, err = yaml.encode({a = {b = 1}})
    if err then error(err) end
    print(encoded)
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

a:
  b: 1

func LVYAMLDecoder added in v0.1.6

func LVYAMLDecoder(L *lua.LState, decoder *yaml.Decoder) lua.LValue

func LVYAMLEncoder added in v0.1.6

func LVYAMLEncoder(L *lua.LState, encoder *yaml.Encoder) lua.LValue

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func Preload

func Preload(L *lua.LState)

Preload adds yaml to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local yaml = require("yaml")

Types

This section is empty.

Jump to

Keyboard shortcuts

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