Documentation ¶
Overview ¶
Package properties provide Java Properties format contents parse, marshal and unmarshal library.
Example ¶
package main import ( "fmt" "github.com/gookit/properties" ) func main() { text := ` # properties string name = inhere age = 200 ` p, err := properties.Parse(text) if err != nil { panic(err) } type MyConf struct { Name string `properties:"name"` Age int `properties:"age"` } cfg := &MyConf{} err = p.MapStruct("", cfg) if err != nil { panic(err) } fmt.Println(*cfg) }
Output: {inhere 200}
Index ¶
- Constants
- Variables
- func Decode(v []byte, ptr any) error
- func Encode(v any) ([]byte, error)
- func Marshal(v any) ([]byte, error)
- func ParseEnv(opts *Options)
- func ParseInlineSlice(opts *Options)
- func ParseTime(opts *Options)
- func Unmarshal(v []byte, ptr any) error
- func ValDecodeHookFunc() mapstructure.DecodeHookFunc
- func WithDebug(opts *Options)
- type Encoder
- type OpFunc
- type Options
- type Parser
- func (p *Parser) Comments() map[string]string
- func (p *Parser) Decode(ptr any) error
- func (p *Parser) MapStruct(key string, ptr any) error
- func (p *Parser) Parse(text string) error
- func (p *Parser) ParseBytes(bs []byte) error
- func (p *Parser) ParseFrom(r io.Reader) error
- func (p *Parser) SMap() maputil.SMap
- func (p *Parser) Unmarshal(v []byte, ptr any) error
- func (p *Parser) WithOptions(optFns ...OpFunc) *Parser
Examples ¶
Constants ¶
View Source
const ( MultiLineValMarkS = "'''" MultiLineValMarkD = `"""` MultiLineValMarkQ = "\\" MultiLineCmtEnd = "*/" VarRefStartChars = "${" )
special chars consts
Variables ¶
View Source
var DefaultTagName = "properties"
DefaultTagName for mapping data to struct.
View Source
var ErrNotFound = errors.New("this key does not exists")
ErrNotFound error
Functions ¶
func Marshal ¶
Marshal data(struct, map) to properties text
Example ¶
package main import ( "fmt" "github.com/gookit/properties" ) func main() { type MyConf struct { Name string `properties:"name"` Age int `properties:"age"` } cfg := &MyConf{ Name: "inhere", Age: 300, } bts, err := properties.Marshal(cfg) if err != nil { panic(err) } fmt.Println(string(bts)) // Output like: // name=inhere // age=300 }
Output:
func ParseInlineSlice ¶
func ParseInlineSlice(opts *Options)
ParseInlineSlice open parse inline slice
func Unmarshal ¶
Unmarshal parse properties text and decode to struct
Example ¶
package main import ( "fmt" "time" "github.com/gookit/properties" ) func main() { text := ` # properties string name = inhere age = 200 project.name = properties project.version = v1.0.1 # parse time string project.cache-time = 10s project.repo.name = ${project.name} project.repo.url = https://github.com/gookit/properties ` type Repo struct { Name string `properties:"name"` URL string `properties:"url"` } type Project struct { Name string `properties:"name"` Version string `properties:"version"` CacheTime time.Duration `properties:"cache-time"` Repo Repo `properties:"repo"` } type MyConf struct { Name string `properties:"name"` Age int `properties:"age"` Project Project `properties:"project"` } cfg := &MyConf{} err := properties.Unmarshal([]byte(text), cfg) if err != nil { panic(err) } fmt.Println(*cfg) }
Output: {inhere 200 {properties v1.0.1 10s {properties https://github.com/gookit/properties}}}
func ValDecodeHookFunc ¶
func ValDecodeHookFunc() mapstructure.DecodeHookFunc
ValDecodeHookFunc returns a mapstructure.DecodeHookFunc that parse time string
Types ¶
type Encoder ¶
type Encoder struct { // TagName for encode a struct. default: properties TagName string // contains filtered or unexported fields }
Encoder struct
type OpFunc ¶
type OpFunc func(opts *Options)
OpFunc custom option func
func WithTagName ¶
WithTagName custom tag name on binding struct
type Options ¶
type Options struct { // Debug open debug mode Debug bool // ParseEnv parse ENV var name, default True. eg: "${SHELL}" ParseEnv bool // ParseVar reference. eg: "${other.var.name}". default: true ParseVar bool // ParseTime string on binding struct. eg: 3s -> 3*time.Second ParseTime bool // TagName for binding data to struct. default: properties TagName string // TrimValue trim "\n" for value string. default: false TrimValue bool // InlineComment support split inline comments. default: false // // allow chars: #, // InlineComment bool // InlineSlice support parse the inline slice. eg: [23, 34]. default: false InlineSlice bool // MapStructConfig for binding data to struct. MapStructConfig mapstructure.DecoderConfig // BeforeCollect value handle func, you can return a new value. BeforeCollect func(name string, val any) any }
Options for config
type Parser ¶
Parser for parse properties contents
func (*Parser) WithOptions ¶
WithOptions for the parser
Click to show internal directories.
Click to hide internal directories.