Last active
October 11, 2016 17:46
-
-
Save ik5/47e9cbbc49dc854aa4c7b64f6f588972 to your computer and use it in GitHub Desktop.
Testing an INI library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Raw configuration: | |
[section] ; comment type of ; ? | |
item = 1 ; comment 2 | |
arr = 1, 4, 5 | |
str = "hey" | |
str2 = 'hey' | |
float = 1.10 | |
[[second section]] # comment type of # ? | |
-: "hello" | |
-: 'world' | |
-: """Multi | |
line""" | |
-: 10:25 | |
[Section 2] | |
comment | |
display = false ; Some comment | |
MapTo: | |
Section items: | |
section: {Item:1 Arr:[1 4 5] Str:hey Str2:hey Float:1.1} | |
item = 1 | |
Arr = [ 1, 4, 5, ] | |
str = hey | |
str2 = hey | |
Float = 1.10 | |
Parsed: | |
Section: DEFAULT | |
Section: section, with comment of : ; comment type of ; ? | |
item = 1 | |
Key comment: ; comment 2 | |
arr = [ | |
1, | |
4, | |
5, | |
] | |
str = hey | |
str2 = hey | |
float = 1.10 | |
Section: [second section], with comment of : # comment type of # ? | |
#1 = hello | |
#2 = world | |
#3 = Multi | |
line | |
#4 = 10:25 | |
Section: Section 2 | |
comment = << true >> | |
display = << false >> | |
Key comment: ; Some comment | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"github.com/go-ini/ini" | |
) | |
const ( | |
// Reset an ANSI color | |
Reset = "\033[0m" | |
// Bold is making a string bold | |
Bold = "\033[1m" | |
// Underline is making a string with underline | |
Underline = "\033[4m" | |
// Blink makes string blink | |
Blink = "\033[5m" | |
// Inverse a string color | |
Inverse = "\033[7m" | |
) | |
// SectionStruct is an attempt to work with mapto to struct | |
type SectionStruct struct { | |
Item int64 `ini:"item"` | |
Arr []int64 `ini:"arr"` | |
Str string `ini:"str"` | |
Str2 string `ini:"str2"` | |
Float float64 `ini:"float"` | |
} | |
func main() { | |
content := `[section] ; comment type of ; ? | |
item = 1 ; comment 2 | |
arr = 1, 4, 5 | |
str = "hey" | |
str2 = 'hey' | |
float = 1.10 | |
[[second section]] # comment type of # ? | |
-: "hello" | |
-: 'world' | |
-: """Multi | |
line""" | |
-: 10:25 | |
[Section 2] | |
comment | |
display = false ; Some comment | |
` | |
cfg, err := ini.LoadSources(ini.LoadOptions{AllowBooleanKeys: true}, []byte(content)) | |
if err != nil { | |
fmt.Println("Error: ", err) | |
return | |
} | |
fmt.Printf("\n%s%sRaw configuration:%s\n%s\n", Bold, Underline, Reset, content) | |
fmt.Printf("%s%sMapTo:%s\n", Bold, Underline, Reset) | |
section := SectionStruct{} | |
err = cfg.Section("section").MapTo(§ion) | |
if err != nil { | |
fmt.Println("Unable to map section: ", err) | |
return | |
} | |
fmt.Printf("\t%sSection items:%s\n", Underline, Reset) | |
fmt.Printf("\t\tsection: %+v\n", section) | |
fmt.Printf("\t\titem = %d\n", section.Item) | |
fmt.Print("\t\tArr = [ ") | |
for _, item := range section.Arr { | |
fmt.Printf("%d, ", item) | |
} | |
fmt.Println("]") | |
fmt.Printf("\t\tstr = %s\n", section.Str) | |
fmt.Printf("\t\tstr2 = %s\n", section.Str2) | |
fmt.Printf("\t\tFloat = %.2f\n", section.Float) | |
fmt.Printf("\n%s%sParsed:%s\n", Bold, Underline, Reset) | |
for _, section := range cfg.Sections() { | |
/* if section.Name() == "DEFAULT" { | |
continue | |
} */ | |
fmt.Printf("Section: %+s", section.Name()) | |
if section.Comment != "" { | |
fmt.Printf(", with comment of : %s %s %s", Inverse, section.Comment, Reset) | |
} | |
fmt.Println("") | |
keys := section.Keys() | |
if len(keys) > 0 { | |
for _, key := range keys { | |
name := strings.Trim(key.Name(), "\n") | |
fmt.Printf("\t %s = ", name) | |
if name == "arr" { | |
items := section.Key(name).Int64s(",") | |
if len(items) > 0 { | |
fmt.Println("[ ") | |
for _, item := range items { | |
fmt.Printf("\t\t %d,\n", item) | |
} | |
fmt.Printf("\t%8s\n", "]") | |
} else { | |
fmt.Println("< Empty arry >") | |
} | |
} else { | |
if i, err := key.Int64(); err == nil { | |
fmt.Printf("%d\n", i) | |
} else if b, err := key.Bool(); err == nil { | |
fmt.Printf("<< %t >>\n", b) | |
} else { | |
fmt.Printf("%s\n", key.Value()) | |
} | |
} | |
if key.Comment != "" { | |
fmt.Printf("\t\t\tKey comment: %s %s %s\n", Inverse, key.Comment, Reset) | |
} | |
} | |
fmt.Println("") | |
} | |
} | |
fmt.Println("") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment