goconf

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: MIT Imports: 8 Imported by: 7

README

goconf

Use Go file as config file.

a := struct {
	Foo string `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.`
	Hello string
}{
	"Bar", "World",
}
c, err := goconf.Marshal(a)
if err != nil {
	panic(err)
}
fmt.Println(string(c))
// Output:
// package config
//
// const (
// 	// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
// 	// incididunt ut labore et dolore magna aliqua.
// 	Foo = "Bar"
//
// 	Hello = "World"
// )

For custom data types, Marshal() uses its String() method, Unmarshal() uses its SetString() method.

Examples:

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNotStruct = errors.New("target must be a struct or a pointer to a struct")
)

Functions

func Marshal

func Marshal(c interface{}) ([]byte, error)

Marshal collects exported keys, values and comments (tag values) of a struct and puts them in a Go file with constant strings.

Example
a := struct {
	Foo string `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.`
	Hello  string
	Bool   bool
	Number int
	Uint   uint32
	Float  float64
}{
	"Bar", "World", true, 123, 22, 1.23,
}
c, err := Marshal(a)
if err != nil {
	panic(err)
}
fmt.Println(string(c))
b, err := json.MarshalIndent(ToConfigs(a), "", "  ")
if err != nil {
	panic(err)
}
fmt.Println(string(b))
Output:

package config

const (
	// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
	// incididunt ut labore et dolore magna aliqua.
	Foo = "Bar"

	Hello = "World"

	Bool = true

	Number = 123

	Uint = 22

	Float = 1.23
)

[
  {
    "Key": "Foo",
    "Value": "Bar",
    "Comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\nincididunt ut labore et dolore magna aliqua."
  },
  {
    "Key": "Hello",
    "Value": "World",
    "Comment": ""
  },
  {
    "Key": "Bool",
    "Value": "true",
    "Comment": ""
  },
  {
    "Key": "Number",
    "Value": "123",
    "Comment": ""
  },
  {
    "Key": "Uint",
    "Value": "22",
    "Comment": ""
  },
  {
    "Key": "Float",
    "Value": "1.23",
    "Comment": ""
  }
]

func Unmarshal

func Unmarshal(input []byte, v interface{}) error

Unmarshal gets all constants in a Go file and assign them to the struct provided.

Example
var c struct {
	Foo    string
	Hello  string
	Bool   bool
	Number int
	Uint   uint32
	Float  float64
}
fmt.Printf("before: %+v\n", c)
err := Unmarshal([]byte(`
package config

const Foo = "BAR"

const (
	Hello  = "WORLD"
	Bool   = true
	Number = 123
	Uint   = 22
	Float  = 1.23
)
`), &c)
if err != nil {
	panic(err)
}
fmt.Printf("after: %+v\n", c)
Output:

before: {Foo: Hello: Bool:false Number:0 Uint:0 Float:0}
after: {Foo:BAR Hello:WORLD Bool:true Number:123 Uint:22 Float:1.23}

Types

type CanSetString added in v1.2.0

type CanSetString interface {
	SetString(string) error
}

type Config added in v1.1.1

type Config struct {
	Key     string
	Value   string
	Comment string
}

func ToConfigs added in v1.1.1

func ToConfigs(c interface{}) (configs []Config)

ToConfigs parses a struct and returns list of its keys, values and comments (struct tag values).

Jump to

Keyboard shortcuts

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