move utility method
Brian Tiger Chow committed
Jan 12, 2015 at 18:23 UTC
bbaf70974dad6552090f77ba62ebf1679dbd495d
3 files changed
+15
-14
repo/config/config.go
+14
@@ -2,9 +2,11 @@
2
package config
3
4
import (
5
+ "bytes"
6
"encoding/base64"
7
"encoding/json"
8
"errors"
9
+ "fmt"
10
"os"
11
"path/filepath"
12
"strings"
@@ -205,3 +207,15 @@ func Marshal(value interface{}) ([]byte, error) {
207
// need to prettyprint, hence MarshalIndent, instead of Encoder
208
return json.MarshalIndent(value, "", " ")
209
}
210
+
211
+func FromMap(v map[string]interface{}) (*Config, error) {
212
+ var buf bytes.Buffer
213
+ if err := json.NewEncoder(&buf).Encode(v); err != nil {
214
+ return nil, err
215
+ }
216
+ var conf Config
217
+ if err := json.NewDecoder(&buf).Decode(&conf); err != nil {
218
+ return nil, fmt.Errorf("Failure to decode config: %s", err)
219
+ }
220
+ return &conf, nil
221
+}
repo/fsrepo/fsrepo.go
+1
-1
@@ -149,7 +149,7 @@ func (r *FSRepo) SetConfigKey(key string, value interface{}) error {
149
if err := writeConfigFile(filename, mapconf); err != nil {
150
return err
151
}
152
- conf, err := convertMapToConfig(mapconf)
152
+ conf, err := config.FromMap(mapconf)
153
if err != nil {
154
return err
155
}
repo/fsrepo/serialize.go
-13
@@ -1,7 +1,6 @@
1
package fsrepo
2
3
import (
4
- "bytes"
4
"encoding/json"
5
"fmt"
6
"io"
@@ -73,18 +72,6 @@ func encode(w io.Writer, value interface{}) error {
72
return err
73
}
74
76
-func convertMapToConfig(v map[string]interface{}) (*config.Config, error) {
77
- var buf bytes.Buffer
78
- if err := json.NewEncoder(&buf).Encode(v); err != nil {
79
- return nil, err
80
- }
81
- var conf config.Config
82
- if err := json.NewDecoder(&buf).Decode(&conf); err != nil {
83
- return nil, fmt.Errorf("Failure to decode config: %s", err)
84
- }
85
- return &conf, nil
86
-}
87
-
75
// load reads given file and returns the read config, or error.
76
func load(filename string) (*config.Config, error) {
77
// if nothing is there, fail. User must run 'ipfs init'