Skip to content

Commit

Permalink
+ option
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Jan 14, 2022
1 parent 64d177f commit 771d7f2
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
8 changes: 4 additions & 4 deletions helper/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func TestProcessMapToArray(t *testing.T) {
"bar": "Value2",
}).Run().([]map[string]interface{})

assert.Equal(t, arr[0]["key"], "foo")
assert.Equal(t, arr[0]["value"], "Value1")
assert.Equal(t, arr[1]["key"], "bar")
assert.Equal(t, arr[1]["value"], "Value2")
assert.Len(t, arr, 2)

assert.True(t, arr[0]["key"] == "foo" || arr[0]["key"] == "bar")
assert.True(t, arr[0]["value"] == "Value1" || arr[0]["value"] == "Value2")
}
14 changes: 7 additions & 7 deletions imports/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

var data = map[string][]byte{
var testDataColumns = map[string][]byte{
"normal": []byte(`{
"label": "订单号",
"name": "order_sn",
Expand Down Expand Up @@ -41,9 +41,9 @@ var data = map[string][]byte{
}`),
}

func TestUnmarshalJSON(t *testing.T) {
func TestColumnUnmarshalJSON(t *testing.T) {
var normal Column
err := jsoniter.Unmarshal(data["normal"], &normal)
err := jsoniter.Unmarshal(testDataColumns["normal"], &normal)
assert.Nil(t, err)
assert.Equal(t, "订单号", normal.Label)
assert.Equal(t, "order_sn", normal.Name)
Expand All @@ -56,7 +56,7 @@ func TestUnmarshalJSON(t *testing.T) {
assert.Equal(t, false, normal.IsObject)

var object Column
err = jsoniter.Unmarshal(data["object"], &object)
err = jsoniter.Unmarshal(testDataColumns["object"], &object)
assert.Nil(t, err)
assert.Equal(t, "性别", object.Label)
assert.Equal(t, "user", object.Name)
Expand All @@ -69,7 +69,7 @@ func TestUnmarshalJSON(t *testing.T) {
assert.Equal(t, false, object.Primary)

var array Column
err = jsoniter.Unmarshal(data["array"], &array)
err = jsoniter.Unmarshal(testDataColumns["array"], &array)
assert.Nil(t, err)
assert.Equal(t, "库存", array.Label)
assert.Equal(t, "stock", array.Name)
Expand All @@ -82,7 +82,7 @@ func TestUnmarshalJSON(t *testing.T) {
assert.Equal(t, false, array.Primary)

var arrayObject Column
err = jsoniter.Unmarshal(data["arrayObject"], &arrayObject)
err = jsoniter.Unmarshal(testDataColumns["arrayObject"], &arrayObject)
assert.Nil(t, err)
assert.Nil(t, err)
assert.Equal(t, "商品", arrayObject.Label)
Expand All @@ -96,7 +96,7 @@ func TestUnmarshalJSON(t *testing.T) {
assert.Equal(t, false, arrayObject.Primary)

var failure Column
err = jsoniter.Unmarshal(data["failure"], &failure)
err = jsoniter.Unmarshal(testDataColumns["failure"], &failure)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), `"label" format is incorrect`)
}
59 changes: 59 additions & 0 deletions imports/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package imports

import (
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/any"
)

// UnmarshalJSON for json marshalJSON
func (option *Option) UnmarshalJSON(source []byte) error {
var data = map[string]interface{}{}
err := jsoniter.Unmarshal(source, &data)
if err != nil {
return err
}

new, err := OptionOf(data)
if err != nil {
return err
}

*option = *new
return nil
}

// OptionOf 解析配置
func OptionOf(data map[string]interface{}) (*Option, error) {
option := &Option{
AutoMatching: true,
ChunkSize: 500,
MappingPreview: PreviewAuto,
DataPreview: PreviewAuto,
}

if autoMatching, ok := data["autoMatching"].(bool); ok {
option.AutoMatching = autoMatching
}

chunkSize := any.Of(data["chunkSize"]).CInt()
if chunkSize > 0 && chunkSize < 2000 {
option.ChunkSize = chunkSize
}

if mappingPreview, ok := data["mappingPreview"].(string); ok {
option.MappingPreview = getPreviewOption(mappingPreview)
}

if dataPreview, ok := data["dataPreview"].(string); ok {
option.DataPreview = getPreviewOption(dataPreview)
}

return option, nil
}

func getPreviewOption(value string) string {
if value != PreviewAlways && value != PreviewAuto && value != PreviewNever {
return PreviewAuto
}
return value
}
41 changes: 41 additions & 0 deletions imports/option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package imports

import (
"testing"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

var testDataOption = map[string][]byte{
"normal": []byte(`{
"autoMatching": true,
"chunkSize":200,
"mappingPreview": "always",
"dataPreview": "never"
}`),
"defaults": []byte(`{}`),
"failure": []byte(`""`),
}

func TestOptionUnmarshalJSON(t *testing.T) {
var normal Option
err := jsoniter.Unmarshal(testDataOption["normal"], &normal)
assert.Nil(t, err)
assert.Equal(t, true, normal.AutoMatching)
assert.Equal(t, 200, normal.ChunkSize)
assert.Equal(t, "always", normal.MappingPreview)
assert.Equal(t, "never", normal.DataPreview)

var defaults Option
err = jsoniter.Unmarshal(testDataOption["defaults"], &defaults)
assert.Nil(t, err)
assert.Equal(t, true, defaults.AutoMatching)
assert.Equal(t, 500, defaults.ChunkSize)
assert.Equal(t, "auto", defaults.MappingPreview)
assert.Equal(t, "auto", defaults.DataPreview)

var failure Option
err = jsoniter.Unmarshal(testDataOption["failure"], &failure)
assert.NotNil(t, err)
}
5 changes: 5 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ repace() {
sed -E "$1" ~/Code/yao/xiang/shell/install.sh > ~/Code/yao/xiang/shell/install.sh.new
}

# 关闭代理
all_proxy=""
http_proxy=""
https_proxy=""

VERSION=$(go run . version)
ssh max@demo-crm.iqka.com mkdir -p /data/demo-crm/ui/releases/$VERSION/
ssh max@demo-crm.iqka.com "echo {\\\"version\\\":\\\"$VERSION\\\"} > /data/demo-crm/ui/releases/latest.json"
Expand Down

0 comments on commit 771d7f2

Please sign in to comment.