Skip to content

Commit 32a1941

Browse files
committed
Merge pull request #91 from lcowell/encode-decode
add config.Encode/Decode
2 parents 3aa1c13 + c230a42 commit 32a1941

2 files changed

Lines changed: 49 additions & 28 deletions

File tree

config/config.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package config
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"os"
88
"path/filepath"
99
"runtime"
@@ -35,13 +35,13 @@ type Config struct {
3535

3636
// ToFile writes a Config to a JSON file.
3737
func ToFile(path string, c Config) error {
38-
sanitize(&c)
39-
bytes, err := json.Marshal(c)
38+
f, err := os.Create(path) // truncates existing file if it exists
4039
if err != nil {
4140
return err
4241
}
42+
defer f.Close()
4343

44-
err = ioutil.WriteFile(path, bytes, 0644)
44+
err = Encode(f, c)
4545
if err != nil {
4646
return err
4747
}
@@ -51,18 +51,30 @@ func ToFile(path string, c Config) error {
5151

5252
// FromFile loads a Config object from a JSON file.
5353
func FromFile(path string) (c Config, err error) {
54-
bytes, err := ioutil.ReadFile(path)
54+
f, err := os.Open(path)
5555
if err != nil {
56-
return
56+
return c, err
5757
}
58+
defer f.Close()
59+
return Decode(f)
60+
}
61+
62+
func Encode(w io.Writer, c Config) error {
63+
sanitize(&c)
64+
e := json.NewEncoder(w)
65+
return e.Encode(c)
66+
}
5867

59-
err = json.Unmarshal(bytes, &c)
68+
func Decode(r io.Reader) (Config, error) {
69+
d := json.NewDecoder(r)
70+
var c Config
71+
err := d.Decode(&c)
6072
if err != nil {
61-
return
73+
return c, err
6274
}
6375
sanitize(&c)
6476

65-
return
77+
return c, err
6678
}
6779

6880
// HomeDir return's the user's canonical home directory.

config/config_test.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"bytes"
45
"io/ioutil"
56
"os"
67
"path/filepath"
@@ -34,41 +35,49 @@ func TestReadingWritingConfig(t *testing.T) {
3435
filename := Filename(tmpDir)
3536
assert.NoError(t, err)
3637

37-
currentConfig := Config{
38-
GithubUsername: "user",
39-
APIKey: "MyKey",
40-
ExercismDirectory: "/exercism/directory",
41-
Hostname: "localhost\r\n",
42-
}
43-
sanitizedConfig := Config{
38+
writtenConfig := Config{
4439
GithubUsername: "user",
4540
APIKey: "MyKey",
4641
ExercismDirectory: "/exercism/directory",
4742
Hostname: "localhost",
4843
}
4944

50-
ToFile(filename, currentConfig)
45+
ToFile(filename, writtenConfig)
5146

5247
loadedConfig, err := FromFile(filename)
5348
assert.NoError(t, err)
5449

55-
assert.Equal(t, sanitizedConfig, loadedConfig)
50+
assert.Equal(t, writtenConfig, loadedConfig)
5651
}
5752

58-
func TestSanitizeFields(t *testing.T) {
59-
config := Config{
60-
GithubUsername: "user ",
61-
APIKey: "MyKey ",
62-
ExercismDirectory: "/home/user name\r\n",
63-
Hostname: "localhost\n",
64-
}
53+
func TestDecodingConfig(t *testing.T) {
54+
unsanitizedJson := `{"githubUsername":"user ","apiKey":"MyKey ","exercismDirectory":"/exercism/directory\r\n","hostname":"localhost \r\n"}`
6555
sanitizedConfig := Config{
6656
GithubUsername: "user",
6757
APIKey: "MyKey",
68-
ExercismDirectory: "/home/user name",
58+
ExercismDirectory: "/exercism/directory",
6959
Hostname: "localhost",
7060
}
71-
sanitize(&config)
61+
b := bytes.NewBufferString(unsanitizedJson)
62+
c, err := Decode(b)
7263

73-
assert.Equal(t, config, sanitizedConfig)
64+
assert.NoError(t, err)
65+
assert.Equal(t, sanitizedConfig, c)
66+
}
67+
68+
func TestEncodingConfig(t *testing.T) {
69+
currentConfig := Config{
70+
GithubUsername: "user\r\n",
71+
APIKey: "MyKey ",
72+
ExercismDirectory: "/home/user name ",
73+
Hostname: "localhost ",
74+
}
75+
sanitizedJson := `{"githubUsername":"user","apiKey":"MyKey","exercismDirectory":"/home/user name","hostname":"localhost"}
76+
`
77+
78+
buf := new(bytes.Buffer)
79+
err := Encode(buf, currentConfig)
80+
81+
assert.NoError(t, err)
82+
assert.Equal(t, sanitizedJson, buf.String())
7483
}

0 commit comments

Comments
 (0)