Documentation ¶
Index ¶
- func FatalOnly(err error) error
- func ReadFileInto(config any, filename string) error
- func ReadInto(config any, reader io.Reader) error
- func ReadStringInto(config any, str string) error
- func ReadWithCallback(reader io.Reader, callback func(string, string, string, string, bool) error) error
- func WarningsOnly(err error) []error
- type Collector
- type List
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FatalOnly ¶
FatalOnly returns the fatal error, if any, **in an error returned by a Collector**. It returns nil if and only if err is nil or err is a List with err.Fatal == nil.
func ReadFileInto ¶
ReadFileInto reads gcfg formatted data from the file filename and sets the values into the corresponding fields in config.
func ReadInto ¶
ReadInto reads gcfg formatted data from reader and sets the values into the corresponding fields in config.
func ReadStringInto ¶
ReadStringInto reads gcfg formatted data from str and sets the values into the corresponding fields in config.
Example ¶
package main import ( "fmt" "log" "github.com/antgroup/hugescm/modules/gcfg" ) func main() { cfgStr := `; Comment line [section] name=value # comment` cfg := struct { Section struct { Name string } }{} err := gcfg.ReadStringInto(&cfg, cfgStr) if err != nil { log.Fatalf("Failed to parse gcfg data: %s", err) } fmt.Println(cfg.Section.Name) }
Output: value
Example (Bool) ¶
package main import ( "fmt" "log" "github.com/antgroup/hugescm/modules/gcfg" ) func main() { cfgStr := `; Comment line [section] switch=on` cfg := struct { Section struct { Switch bool } }{} err := gcfg.ReadStringInto(&cfg, cfgStr) if err != nil { log.Fatalf("Failed to parse gcfg data: %s", err) } fmt.Println(cfg.Section.Switch) }
Output: true
Example (Hyphens) ¶
package main import ( "fmt" "log" "github.com/antgroup/hugescm/modules/gcfg" ) func main() { cfgStr := `; Comment line [section-name] variable-name=value # comment` cfg := struct { Section_Name struct { Variable_Name string } }{} err := gcfg.ReadStringInto(&cfg, cfgStr) if err != nil { log.Fatalf("Failed to parse gcfg data: %s", err) } fmt.Println(cfg.Section_Name.Variable_Name) }
Output: value
Example (Multivalue) ¶
package main import ( "fmt" "log" "github.com/antgroup/hugescm/modules/gcfg" ) func main() { cfgStr := `; Comment line [section] multi=value1 multi=value2` cfg := struct { Section struct { Multi []string } }{} err := gcfg.ReadStringInto(&cfg, cfgStr) if err != nil { log.Fatalf("Failed to parse gcfg data: %s", err) } fmt.Println(cfg.Section.Multi) }
Output: [value1 value2]
Example (Subsections) ¶
package main import ( "fmt" "log" "github.com/antgroup/hugescm/modules/gcfg" ) func main() { cfgStr := `; Comment line [profile "A"] color = white [profile "B"] color = black ` cfg := struct { Profile map[string]*struct { Color string } }{} err := gcfg.ReadStringInto(&cfg, cfgStr) if err != nil { log.Fatalf("Failed to parse gcfg data: %s", err) } fmt.Printf("%s %s\n", cfg.Profile["A"].Color, cfg.Profile["B"].Color) }
Output: white black
Example (Tags) ¶
package main import ( "fmt" "log" "github.com/antgroup/hugescm/modules/gcfg" ) func main() { cfgStr := `; Comment line [section] var-name=value # comment` cfg := struct { Section struct { FieldName string `gcfg:"var-name"` } }{} err := gcfg.ReadStringInto(&cfg, cfgStr) if err != nil { log.Fatalf("Failed to parse gcfg data: %s", err) } fmt.Println(cfg.Section.FieldName) }
Output: value
Example (Unicode) ¶
package main import ( "fmt" "log" "github.com/antgroup/hugescm/modules/gcfg" ) func main() { cfgStr := `; Comment line [甲] 乙=丙 # comment` cfg := struct { X甲 struct { X乙 string } }{} err := gcfg.ReadStringInto(&cfg, cfgStr) if err != nil { log.Fatalf("Failed to parse gcfg data: %s", err) } fmt.Println(cfg.X甲.X乙) }
Output: 丙
func ReadWithCallback ¶
func ReadWithCallback(reader io.Reader, callback func(string, string, string, string, bool) error) error
ReadWithCallback reads gcfg formatted data from reader and calls callback with each section and option found.
Callback is called with section, subsection, option key, option value and blank value flag as arguments.
When a section is found, callback is called with nil subsection, option key and option value.
When a subsection is found, callback is called with nil option key and option value.
If blank value flag is true, it means that the value was not set for an option (as opposed to set to empty string).
If callback returns an error, ReadWithCallback terminates with an error too.
func WarningsOnly ¶
WarningsOnly returns the warnings **in an error returned by a Collector**.
Types ¶
type Collector ¶
type Collector struct { // IsFatal distinguishes between warnings and fatal errors. IsFatal func(error) bool // FatalWithWarnings set to true means that a fatal error is returned as // a List together with all warnings so far. The default behavior is to // only return the fatal error and discard any warnings that have been // collected. FatalWithWarnings bool // contains filtered or unexported fields }
A Collector collects errors up to the first fatal error.
func NewCollector ¶
NewCollector returns a new Collector; it uses isFatal to distinguish between warnings and fatal errors.
Directories ¶
Path | Synopsis |
---|---|
Package scanner implements a scanner for gcfg configuration text.
|
Package scanner implements a scanner for gcfg configuration text. |
Package token defines constants representing the lexical tokens of the gcfg configuration syntax and basic operations on tokens (printing, predicates).
|
Package token defines constants representing the lexical tokens of the gcfg configuration syntax and basic operations on tokens (printing, predicates). |
Package types defines helpers for type conversions.
|
Package types defines helpers for type conversions. |