diff --git a/helper/map_test.go b/helper/map_test.go index e2174d9fb6..c7c6d52eab 100644 --- a/helper/map_test.go +++ b/helper/map_test.go @@ -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") } diff --git a/imports/column_test.go b/imports/column_test.go index e2143f3591..f8c46d6928 100644 --- a/imports/column_test.go +++ b/imports/column_test.go @@ -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", @@ -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) @@ -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) @@ -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) @@ -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) @@ -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`) } diff --git a/imports/option.go b/imports/option.go new file mode 100644 index 0000000000..20a38605e5 --- /dev/null +++ b/imports/option.go @@ -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 +} diff --git a/imports/option_test.go b/imports/option_test.go new file mode 100644 index 0000000000..b8be7f58f7 --- /dev/null +++ b/imports/option_test.go @@ -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) +} diff --git a/release.sh b/release.sh index ba08fe7da8..3c832cd33b 100755 --- a/release.sh +++ b/release.sh @@ -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"