Back to godoc.org
github.com/binarydud/hclencoder

Package hclencoder

v0.0.0-...-6b94726
Latest Go to latest

The highest tagged major version is .

Published: Jun 7, 2018 | License: MIT | Module: github.com/binarydud/hclencoder

Overview

Example

Code:

type Farm struct {
	Name     string    `hcl:"name"`
	Owned    bool      `hcl:"owned"`
	Location []float64 `hcl:"location"`
}

type Farmer struct {
	Name                 string `hcl:"name"`
	Age                  int    `hcl:"age"`
	SocialSecurityNumber string `hcle:"omit"`
}

type Animal struct {
	Name  string `hcl:",key"`
	Sound string `hcl:"says" hcle:"omitempty"`
}

type Pet struct {
	Species string `hcl:",key"`
	Name    string `hcl:",key"`
	Sound   string `hcl:"says" hcle:"omitempty"`
}

type Config struct {
	Farm      `hcl:",squash"`
	Farmer    Farmer            `hcl:"farmer"`
	Animals   []Animal          `hcl:"animal"`
	Pets      []Pet             `hcl:"pet"`
	Buildings map[string]string `hcl:"buildings"`
}

input := Config{
	Farm: Farm{
		Name:     "Ol' McDonald's Farm",
		Owned:    true,
		Location: []float64{12.34, -5.67},
	},
	Farmer: Farmer{
		Name:                 "Robert Beauregard-Michele McDonald, III",
		Age:                  65,
		SocialSecurityNumber: "please-dont-share-me",
	},
	Animals: []Animal{
		{
			Name:  "cow",
			Sound: "moo",
		},
		{
			Name:  "pig",
			Sound: "oink",
		},
		{
			Name: "rock",
		},
	},
	Pets: []Pet{
		{
			Species: "cat",
			Name:    "whiskers",
			Sound:   "meow",
		},
	},
	Buildings: map[string]string{
		"House": "123 Numbers Lane",
		"Barn":  "456 Digits Drive",
	},
}

hcl, err := Encode(input)
if err != nil {
	log.Fatal("unable to encode: ", err)
}

fmt.Print(string(hcl))
name = "Ol' McDonald's Farm"

owned = true

location = [
  12.34,
  -5.67,
]

farmer {
  name = "Robert Beauregard-Michele McDonald, III"
  age  = 65
}

animal "cow" {
  says = "moo"
}

animal "pig" {
  says = "oink"
}

animal "rock" {}

pet "cat" "whiskers" {
  says = "meow"
}

buildings {
  Barn  = "456 Digits Drive"
  House = "123 Numbers Lane"
}

Index

Examples

Constants

const (
	// HCLTagName is the struct field tag used by the HCL decoder. The
	// values from this tag are used in the same way as the decoder.
	HCLTagName = "hcl"

	// KeyTag indicates that the value of the field should be part of
	// the parent object block's key, not a property of that block
	KeyTag string = "key"

	// SquashTag is attached to anonymous fields of a struct and indicates
	// to the encoder to lift the fields of that value into the parent
	// block's scope transparently. Otherwise, the field's type is used as
	// the key for the value.
	SquashTag string = "squash"

	// UnusedKeysTag is a flag that indicates any unused keys found by the
	// decoder are stored in this field of type []string. This has the same
	// behavior as the OmitTag and is not encoded.
	UnusedKeysTag string = "unusedKeys"

	// DecodedFieldsTag is a flag that indicates all fields decoded are
	// stored in this field of type []string. This has the same behavior as
	// the OmitTag and is not encoded.
	DecodedFieldsTag string = "decodedFields"

	// HCLETagName is the struct field tag used by this package. The
	// values from this tag are used in conjunction with HCLTag values.
	HCLETagName = "hcle"

	// OmitTag will omit this field from encoding. This is the similar
	// behavior to `json:"-"`.
	OmitTag string = "omit"

	// OmitEmptyTag will omit this field if it is a zero value. This
	// is similar behavior to `json:",omitempty"`
	OmitEmptyTag string = "omitempty"
)

func Encode

func Encode(in interface{}) ([]byte, error)

Encode converts any supported type into the corresponding HCL format

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier