@cryptotaxi247 / kubo / commits / ea5243400

go-ipfs-config: move utility method

Brian Tiger Chow committed Jan 12, 2015 at 18:23 UTC ea5243400c1e391cfd5c32c135568dcc4bb6874a
1 file changed +14
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 +}