github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/encoding/ini/dict.go (about) 1 // Copyright 2012 <chaishushan{AT}gmail.com>. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ini 6 7 import ( 8 "bytes" 9 "errors" 10 "fmt" 11 "sort" 12 "strconv" 13 "strings" 14 "unicode" 15 ) 16 17 type Dict map[string]map[string]string 18 19 func newDict() Dict { 20 dict := make(map[string]map[string]string) 21 return Dict(dict) 22 } 23 24 func (dict Dict) GetSections() []string { 25 sections := make([]string, 0, len(dict)) 26 for sec, _ := range dict { 27 sections = append(sections, sec) 28 } 29 sort.StringSlice(sections).Sort() 30 return sections 31 } 32 func (dict Dict) GetKeys(section string) []string { 33 sec, ok := dict[section] 34 if !ok { 35 return nil 36 } 37 keys := make([]string, 0, len(sec)) 38 for key, _ := range sec { 39 keys = append(keys, key) 40 } 41 sort.StringSlice(keys).Sort() 42 return keys 43 } 44 45 func (dict Dict) GetString(section, key string) (string, error) { 46 sec, ok := dict[section] 47 if !ok { 48 return "", errors.New("Section Not Found") 49 } 50 value, ok := sec[key] 51 if !ok { 52 return "", errors.New("Key Not Found") 53 } 54 return value, nil 55 } 56 57 func (dict Dict) GetBool(section, key string) (bool, error) { 58 sec, ok := dict[section] 59 if !ok { 60 return false, errors.New("Section Not Found") 61 } 62 value, ok := sec[key] 63 if !ok { 64 return false, errors.New("Key Not Found") 65 } 66 v := value[0] 67 if v == 'y' || v == 'Y' || v == '1' || v == 't' || v == 'T' { 68 return true, nil 69 } 70 if v == 'n' || v == 'N' || v == '0' || v == 'f' || v == 'F' { 71 return false, nil 72 } 73 return false, errors.New("Parse Bool Failed") 74 } 75 76 func (dict Dict) GetInt(section, key string) (int, error) { 77 sec, ok := dict[section] 78 if !ok { 79 return 0, errors.New("Section Not Found") 80 } 81 value, ok := sec[key] 82 if !ok { 83 return 0, errors.New("Key Not Found") 84 } 85 i, err := strconv.Atoi(value) 86 if err != nil { 87 return 0, errors.New("Parse Int Failed") 88 } 89 return i, nil 90 } 91 92 func (dict Dict) GetFloat(section, key string) (float64, error) { 93 sec, ok := dict[section] 94 if !ok { 95 return 0, errors.New("Section Not Found") 96 } 97 value, ok := sec[key] 98 if !ok { 99 return 0, errors.New("Key Not Found") 100 } 101 d, err := strconv.ParseFloat(value, 64) 102 if err != nil { 103 return 0, errors.New("Parse Float Failed") 104 } 105 return d, nil 106 } 107 108 func (dict Dict) String() string { 109 var b bytes.Buffer 110 sections := dict.GetSections() 111 for _, sec := range sections { 112 if sec != "" { 113 fmt.Fprintf(&b, "[%s]\n", sec) 114 } 115 keys := dict.GetKeys(sec) 116 for _, key := range keys { 117 if key != "" { 118 fmt.Fprintf(&b, "%s=%s\n", key, dict[sec][key]) 119 } 120 } 121 fmt.Fprintf(&b, "\n") 122 } 123 return b.String() 124 } 125 126 func (dict *Dict) setSection(section string) { 127 section = strings.TrimFunc(section, unicode.IsSpace) 128 if section == "" { 129 return 130 } 131 132 if _, ok := (*dict)[section]; !ok { 133 (*dict)[section] = make(map[string]string) 134 } 135 } 136 func (dict *Dict) setString(section, key string, value string) { 137 section = strings.TrimFunc(section, unicode.IsSpace) 138 if section == "" { 139 return 140 } 141 142 if _, ok := (*dict)[section]; !ok { 143 (*dict)[section] = make(map[string]string) 144 } 145 (*dict)[section][key] = value 146 }