1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-09 19:32:24 +08:00

move utility method

This commit is contained in:
Brian Tiger Chow
2015-01-12 18:23:29 -08:00
parent 306c7e2e7b
commit bbaf70974d
3 changed files with 15 additions and 14 deletions

View File

@ -2,9 +2,11 @@
package config
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
@ -205,3 +207,15 @@ func Marshal(value interface{}) ([]byte, error) {
// need to prettyprint, hence MarshalIndent, instead of Encoder
return json.MarshalIndent(value, "", " ")
}
func FromMap(v map[string]interface{}) (*Config, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(v); err != nil {
return nil, err
}
var conf Config
if err := json.NewDecoder(&buf).Decode(&conf); err != nil {
return nil, fmt.Errorf("Failure to decode config: %s", err)
}
return &conf, nil
}