Skip to content

Commit 108bd3e

Browse files
committed
Added LoadFromBytes
1 parent 7e1498e commit 108bd3e

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

properties.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,8 @@ func NewFromHashmap(orig map[string]string) *Map {
126126
return res
127127
}
128128

129-
// Load reads a properties file and makes a Map out of it.
130-
func Load(filepath string) (*Map, error) {
131-
bytes, err := ioutil.ReadFile(filepath)
132-
if err != nil {
133-
return nil, fmt.Errorf("Error reading file: %s", err)
134-
}
135-
129+
// LoadFromBytes reads properties data and makes a Map out of it.
130+
func LoadFromBytes(bytes []byte) (*Map, error) {
136131
text := string(bytes)
137132
text = strings.Replace(text, "\r\n", "\n", -1)
138133
text = strings.Replace(text, "\r", "\n", -1)
@@ -141,13 +136,27 @@ func Load(filepath string) (*Map, error) {
141136

142137
for lineNum, line := range strings.Split(text, "\n") {
143138
if err := properties.parseLine(line); err != nil {
144-
return nil, fmt.Errorf("Error reading file (%s:%d): %s", filepath, lineNum, err)
139+
return nil, fmt.Errorf("Error parsing data at line %d: %s", lineNum, err)
145140
}
146141
}
147142

148143
return properties, nil
149144
}
150145

146+
// Load reads a properties file and makes a Map out of it.
147+
func Load(filepath string) (*Map, error) {
148+
bytes, err := ioutil.ReadFile(filepath)
149+
if err != nil {
150+
return nil, fmt.Errorf("Error reading file: %s", err)
151+
}
152+
153+
res, err := LoadFromBytes(bytes)
154+
if err != nil {
155+
return nil, fmt.Errorf("Error reading file: %s", err)
156+
}
157+
return res, nil
158+
}
159+
151160
// LoadFromPath reads a properties file and makes a Map out of it.
152161
func LoadFromPath(path *paths.Path) (*Map, error) {
153162
return Load(path.String())

properties_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,65 @@ func TestPropertiesRedBeearLabBoardsTxt(t *testing.T) {
136136
require.Equal(t, "arduino:arduino", ethernet.Get("build.core"))
137137
}
138138

139+
func TestLoadFromBytes(t *testing.T) {
140+
data := []byte(`
141+
yun.vid.0=0x2341
142+
yun.pid.0=0x0041
143+
yun.vid.1=0x2341
144+
yun.pid.1=0x8041
145+
yun.upload.tool=avrdude
146+
yun.upload.protocol=avr109
147+
yun.upload.maximum_size=28672
148+
yun.upload.maximum_data_size=2560
149+
yun.upload.speed=57600
150+
yun.upload.disable_flushing=true
151+
yun.upload.use_1200bps_touch=true
152+
yun.upload.wait_for_upload_port=true
153+
`)
154+
m, err := LoadFromBytes(data)
155+
require.NoError(t, err)
156+
require.Equal(t, "57600", m.Get("yun.upload.speed"))
157+
158+
data2 := []byte(`
159+
yun.vid.0=0x2341
160+
yun.pid.1
161+
yun.upload.tool=avrdude
162+
`)
163+
m2, err2 := LoadFromBytes(data2)
164+
fmt.Println(err2)
165+
require.Error(t, err2)
166+
require.Nil(t, m2)
167+
}
168+
169+
func TestLoadFromSlice(t *testing.T) {
170+
data := []string{"yun.vid.0=0x2341",
171+
"yun.pid.0=0x0041",
172+
"yun.vid.1=0x2341",
173+
"yun.pid.1=0x8041",
174+
"yun.upload.tool=avrdude",
175+
"yun.upload.protocol=avr109",
176+
"yun.upload.maximum_size=28672",
177+
"yun.upload.maximum_data_size=2560",
178+
"yun.upload.speed=57600",
179+
"yun.upload.disable_flushing=true",
180+
"yun.upload.use_1200bps_touch=true",
181+
"yun.upload.wait_for_upload_port=true",
182+
}
183+
m, err := LoadFromSlice(data)
184+
require.NoError(t, err)
185+
require.Equal(t, "57600", m.Get("yun.upload.speed"))
186+
187+
data2 := []string{
188+
"yun.vid.0=0x2341",
189+
"yun.pid.1",
190+
"yun.upload.tool=avrdude",
191+
}
192+
193+
m2, err2 := LoadFromSlice(data2)
194+
fmt.Println(err2)
195+
require.Error(t, err2)
196+
require.Nil(t, m2)
197+
}
139198
func TestSubTreeForMultipleDots(t *testing.T) {
140199
p := NewMap()
141200
p.Set("root.lev1.prop", "hi")

0 commit comments

Comments
 (0)