yaml

package module
v0.0.0-...-70aca8a Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2022 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

yaml package is a goschtalt decoder package.

The yaml package automatically registers the decoder as a default decoder with the goschtalt package so the usage is as simple as possible ... simply import the package and it should just work.

Import the package like you do for pprof - like this:

import (
	"fmt"
	"os"
	...

	"github.com/schmidtw/goschtalt"
	_ "github.com/schmidtw/goschtalt/extensions/decoders/yaml"
)

See the example for how to use this extension package.

Example
// SPDX-FileCopyrightText: 2022 Weston Schmidt <weston_schmidt@alumni.purdue.edu>
// SPDX-License-Identifier: Apache-2.0

package main

import (
	"fmt"
	"io/fs"
	"strings"

	"github.com/psanford/memfs"
	"github.com/schmidtw/goschtalt"
	_ "github.com/schmidtw/goschtalt/extensions/decoders/yaml"
)

const filename = `example.yml`
const text = `---
example:
    version: 1
    colors: [red, green, blue]`

func getFS() fs.FS {
	mfs := memfs.New()
	if err := mfs.WriteFile(filename, []byte(text), 0755); err != nil {
		panic(err)
	}

	return mfs
}

func main() {
	// Normally, you use something like os.DirFS("/etc/program")
	g, err := goschtalt.New(goschtalt.AddDir(getFS(), "."))
	if err != nil {
		panic(err)
	}

	err = g.Compile()
	if err != nil {
		panic(err)
	}

	var cfg struct {
		Example struct {
			Version int
			Colors  []string
		}
	}

	err = g.Unmarshal("", &cfg)
	if err != nil {
		panic(err)
	}

	fmt.Println("example")
	fmt.Printf("    version = %d\n", cfg.Example.Version)
	fmt.Printf("    colors  = [ %s ]\n", strings.Join(cfg.Example.Colors, ", "))

}
Output:

example
    version = 1
    colors  = [ red, green, blue ]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct{}

Decoder is a class for the yaml decoder.

func (Decoder) Decode

func (d Decoder) Decode(ctx decoder.Context, b []byte, m *meta.Object) error

Decode decodes a byte arreay into the meta.Object tree.

func (Decoder) Extensions

func (d Decoder) Extensions() []string

Extensions returns the supported extensions.

Jump to

Keyboard shortcuts

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