~weberc2/goconf/trunk

« back to all changes in this revision

Viewing changes to settingslist.go

  • Committer: weberc2 at gmail
  • Date: 2013-03-03 03:58:13 UTC
  • Revision ID: weberc2@gmail.com-20130303035813-95llzmarv6sjzgng
Added some unit testing

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
package goconf
6
6
 
7
 
import (
8
 
        "errors"
9
 
        "strings"
10
 
)
11
 
 
12
7
/* notFoundError 
13
 
Returned when a specified key can't be found. */
14
 
type notFoundError struct {
15
 
        key string
16
 
}
 
8
Returned when a specified key can't be found. Implements `error`. */
 
9
type notFoundError struct {     key string }
17
10
 
18
11
// creates a new notFoundError
19
12
func newNotFoundError(key string) *notFoundError {
25
18
        return "key `" + this.key + "` not found"
26
19
}
27
20
 
28
 
/* SettingRec
29
 
A record in a settingsList */
30
 
type settingRec struct {
31
 
        key, val string
32
 
}
33
 
 
34
 
func newSettingRecFromString(input string) (*settingRec, error) {
35
 
        this := &settingRec{}
36
 
        if len(input) == 0 { // comment
37
 
                this.key = ""
38
 
                this.val = ""
39
 
        } else if input[0] == '[' || input[0] == '#' { // comment
40
 
                this.key = string(input[0])
41
 
                this.val = input[1:len(input)]
42
 
        } else { // legit settingRec
43
 
                parts := strings.Split(input, "=")
44
 
                if len(parts) != 2 {
45
 
                        return nil, errors.New("`" + input + "` isn't a valid setting")
46
 
                } else {
47
 
                        this.key = parts[0]
48
 
                        this.val = parts[1]
49
 
                }
50
 
        }
51
 
        return this, nil
52
 
}
53
 
 
54
 
func (this *settingRec) Set(val string) {
55
 
        this.val = val
56
 
}
57
 
 
58
 
func (this *settingRec) Key() string {
59
 
        return this.key
60
 
}
61
 
 
62
 
func (this *settingRec) Val() string {
63
 
        return this.val
64
 
}
65
 
 
66
 
func (this *settingRec) IsComment() bool {
67
 
        return len(this.key) == 0 || this.key == "[" || this.key == "#"
68
 
}
69
 
 
70
 
func (this *settingRec) String() string {
71
 
        if this.IsComment() {
72
 
                return this.key + this.val
73
 
        }
74
 
        return this.key + "=" + this.val
75
 
}
76
 
 
77
21
/* settingsList
78
22
A list of settings. This structure acts as a dictionary for
79
23
settings' keys and values; however, it also keeps copies of
139
83
// Add a record to the table. Returns an error if the string can't
140
84
// be parsed into a setting/value pair
141
85
func (this *settingsList) Add(input string) error {
142
 
        newRec, err := newSettingRecFromString(input)
143
 
        if err != nil {
144
 
                return err
145
 
        }
 
86
        newRec, err := newSettingRec(input)
 
87
        if err != nil { return err }
146
88
 
147
 
        // if the new key already exists, do nothing    
148
 
        if this.Contains(newRec) {
149
 
                return nil
150
 
        }
 
89
        // if the new key already exists, do nothing (since all comments have the
 
90
        // same key, this prevents them from overwriting each other. This should
 
91
        // probably be handled better.)
 
92
        if this.Contains(newRec) { return nil }
151
93
 
152
94
        // now that we know it's new, let's check to see if it's a comment. If it
153
95
        // is, we can append the new comment to our list