3
0

Improve setting export/import structure

Properly encode/decode json structures.
This commit is contained in:
Tomaž Jerman
2019-10-14 14:12:02 +02:00
parent 5a4985cbdc
commit 298ce908c2
2 changed files with 15 additions and 7 deletions

View File

@@ -1,15 +1,22 @@
package settings
import "gopkg.in/yaml.v2"
import (
"encoding/json"
"gopkg.in/yaml.v2"
)
// Export transforms a given ValueSet into a yaml exportable structure
func Export(ss ValueSet) (o yaml.MapSlice) {
o = yaml.MapSlice{}
for _, s := range ss {
var v interface{}
json.Unmarshal(s.Value, &v)
setting := yaml.MapItem{
Key: s.Name,
Value: s.Value.String(),
Value: v,
}
o = append(o, setting)
}

View File

@@ -2,11 +2,11 @@ package settings
import (
"context"
"encoding/json"
"github.com/pkg/errors"
"github.com/cortezaproject/corteza-server/pkg/deinterfacer"
"github.com/jmoiron/sqlx/types"
)
type (
@@ -36,14 +36,15 @@ func (imp *Importer) CastSet(settings interface{}) (err error) {
}
func (imp *Importer) addSetting(name string, value interface{}) (err error) {
v, ok := value.(string)
if !ok {
return errors.New("value must be string")
// Convert to interface{}, since json.Marshal cant handle map[interface{}]interface{}
v, err := json.Marshal(deinterfacer.Simplify(value))
if err != nil {
return err
}
setting := &Value{
Name: name,
Value: types.JSONText(v),
Value: v,
}
imp.settings = append(imp.settings, setting)