forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters