ut/utini/file.go

57 lines
910 B
Go

package utini
import (
"sort"
"strings"
)
type Comment string
type File struct {
Sections map[string]*Section
}
func (f *File) CreateIfNotExists(name string) *Section {
if f.Sections == nil {
f.Sections = map[string]*Section{}
}
section := f.Sections[name]
if section == nil {
section = &Section{}
f.Sections[name] = section
}
return section
}
type Property struct {
Key, Value string
}
type Section struct {
Properties []*Property
}
func (s Section) PropertyByKey(key string) *Property {
key = strings.ToLower(key)
for _, p := range s.Properties {
if strings.ToLower(p.Key) == key {
return p
}
}
return nil
}
func (s Section) Keys() []string {
keys := map[string]struct{}{}
for _, property := range s.Properties {
keys[property.Key] = struct{}{}
}
var sorted []string
for key := range keys {
sorted = append(sorted, key)
}
sort.Strings(sorted)
return sorted
}