Skip to content

jsonitor not compatible with encoding/json, unmarshal lose data #217

Closed
@wonderflow

Description

@wonderflow

try this test case:

type MapConf map[string]string

type MetricConfig struct {
	MetricType string                 `json:"type"`
	Attributes map[string]bool        `json:"attributes"`
	Config     map[string]interface{} `json:"config"`
}

type RouterConfig struct {
	KeyName      string         `json:"router_key_name"`
	MatchType    string         `json:"router_match_type"`
	DefaultIndex int            `json:"router_default_sender"`
	Routes       map[string]int `json:"router_routes"`
}

type RunnerConfig struct {
	RunnerInfo
	MetricConfig  []MetricConfig           `json:"metric,omitempty"`
	ReaderConfig  MapConf             `json:"reader"`
	CleanerConfig MapConf             `json:"cleaner,omitempty"`
	ParserConf    MapConf             `json:"parser"`
	Transforms    []map[string]interface{} `json:"transforms,omitempty"`
	SenderConfig  []MapConf           `json:"senders"`
	Router        RouterConfig      `json:"router,omitempty"`
	IsInWebFolder bool                     `json:"web_folder,omitempty"`
	IsStopped     bool                     `json:"is_stopped,omitempty"`
}

type SlaveConfig struct {
	Configs map[string]RunnerConfig `json:"configs"`
	Tag     string                  `json:"tag"`
	Err     error                   `json:"error"`
}

type respSlaveConfig struct {
	Code string                 `json:"code"`
	Data map[string]SlaveConfig `json:"data"`
}

func TestJsoniter(t *testing.T) {
	respGotConfigs1, respGotConfigs2 := respSlaveConfig{}, respSlaveConfig{}
	var teststring = `{"code":"L200","data":{"http://192.168.0.106:6202":{"configs":{"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/slave1/confs/clusterSalveConfigsTest.conf":{"name":"clusterSalveConfigsTest","collect_interval":1,"batch_len":1,"batch_size":200,"batch_interval":1,"batch_try_times":3,"createtime":"2018-01-03T22:25:36.497442704+08:00","reader":{"ignore_hidden":"true","log_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/logdir","meta_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/meta","mode":"dir","name":"clusterSalveConfigsTest","read_from":"oldest","runner_name":"clusterSalveConfigsTest"},"parser":{"name":"json_parser","runner_name":"clusterSalveConfigsTest","type":"json"},"senders":[{"file_send_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/sender/sendData","name":"file_sender","runner_name":"clusterSalveConfigsTest","sender_type":"file"}],"router":{"router_key_name":"","router_match_type":"","router_default_sender":0,"router_routes":null},"web_folder":true}},"tag":"test","error":null},"http://192.168.0.106:6203":{"configs":{"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/slave2/confs/clusterSalveConfigsTest.conf":{"name":"clusterSalveConfigsTest","collect_interval":1,"batch_len":1,"batch_size":200,"batch_interval":1,"batch_try_times":3,"createtime":"2018-01-03T22:25:36.497453622+08:00","reader":{"ignore_hidden":"true","log_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/logdir","meta_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/meta","mode":"dir","name":"clusterSalveConfigsTest","read_from":"oldest","runner_name":"clusterSalveConfigsTest"},"parser":{"name":"json_parser","runner_name":"clusterSalveConfigsTest","type":"json"},"senders":[{"file_send_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/sender/sendData","name":"file_sender","runner_name":"clusterSalveConfigsTest","sender_type":"file"}],"router":{"router_key_name":"","router_match_type":"","router_default_sender":0,"router_routes":null},"web_folder":true}},"tag":"test","error":null}}}`
	err := json.Unmarshal([]byte(teststring), &respGotConfigs1)
	assert.NoError(t, err)
	stjson := jsoniter.ConfigCompatibleWithStandardLibrary
	err = stjson.Unmarshal([]byte(teststring), &respGotConfigs2)
	assert.NoError(t, err)
	assert.Equal(t, respGotConfigs1, respGotConfigs2)
}

finally , I got this errror:

Error Trace:	cluster_test.go:1096
	Error:      	Received unexpected error:
	            	mgr.respSlaveConfig.Data: mgr.SlaveConfig.Err: read non-empty interface: do not know which concrete type to decode to, error found in #10 byte of ...|","error":null},"htt|..., bigger context ...|s":null},"web_folder":true}},"tag":"test","error":null},"http://192.168.0.106:6203":{"configs":{"/Us|...
	Error Trace:	cluster_test.go:1097
	Error:      	Not equal: 
	            	expected: mgr.respSlaveConfig{Code:"L200", Data:map[string]mgr.SlaveConfig{"http://192.168.0.106:6202":mgr.SlaveConfig{Configs:map[string]mgr.RunnerConfig{"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/slave1/confs/clusterSalveConfigsTest.conf":mgr.RunnerConfig{RunnerInfo:mgr.RunnerInfo{RunnerName:"clusterSalveConfigsTest", CollectInterval:1, MaxBatchLen:1, MaxBatchSize:200, MaxBatchInteval:1, MaxBatchTryTimes:3, CreateTime:"2018-01-03T22:25:36.497442704+08:00"}, MetricConfig:[]mgr.MetricConfig(nil), ReaderConfig:conf.MapConf{"name":"clusterSalveConfigsTest", "read_from":"oldest", "runner_name":"clusterSalveConfigsTest", "ignore_hidden":"true", "log_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/logdir", "meta_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/meta", "mode":"dir"}, CleanerConfig:conf.MapConf(nil), ParserConf:conf.MapConf{"name":"json_parser", "runner_name":"clusterSalveConfigsTest", "type":"json"}, Transforms:[]map[string]interface {}(nil), SenderConfig:[]conf.MapConf{conf.MapConf{"name":"file_sender", "runner_name":"clusterSalveConfigsTest", "sender_type":"file", "file_send_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/sender/sendData"}}, Router:sender.RouterConfig{KeyName:"", MatchType:"", DefaultIndex:0, Routes:map[string]int(nil)}, IsInWebFolder:true, IsStopped:false}}, Tag:"test", Err:error(nil)}, "http://192.168.0.106:6203":mgr.SlaveConfig{Configs:map[string]mgr.RunnerConfig{"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/slave2/confs/clusterSalveConfigsTest.conf":mgr.RunnerConfig{RunnerInfo:mgr.RunnerInfo{RunnerName:"clusterSalveConfigsTest", CollectInterval:1, MaxBatchLen:1, MaxBatchSize:200, MaxBatchInteval:1, MaxBatchTryTimes:3, CreateTime:"2018-01-03T22:25:36.497453622+08:00"}, MetricConfig:[]mgr.MetricConfig(nil), ReaderConfig:conf.MapConf{"mode":"dir", "name":"clusterSalveConfigsTest", "read_from":"oldest", "runner_name":"clusterSalveConfigsTest", "ignore_hidden":"true", "log_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/logdir", "meta_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/meta"}, CleanerConfig:conf.MapConf(nil), ParserConf:conf.MapConf{"runner_name":"clusterSalveConfigsTest", "type":"json", "name":"json_parser"}, Transforms:[]map[string]interface {}(nil), SenderConfig:[]conf.MapConf{conf.MapConf{"runner_name":"clusterSalveConfigsTest", "sender_type":"file", "file_send_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/sender/sendData", "name":"file_sender"}}, Router:sender.RouterConfig{KeyName:"", MatchType:"", DefaultIndex:0, Routes:map[string]int(nil)}, IsInWebFolder:true, IsStopped:false}}, Tag:"test", Err:error(nil)}}}
	            	received: mgr.respSlaveConfig{Code:"L200", Data:map[string]mgr.SlaveConfig{"http://192.168.0.106:6202":mgr.SlaveConfig{Configs:map[string]mgr.RunnerConfig{"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/slave1/confs/clusterSalveConfigsTest.conf":mgr.RunnerConfig{RunnerInfo:mgr.RunnerInfo{RunnerName:"clusterSalveConfigsTest", CollectInterval:1, MaxBatchLen:1, MaxBatchSize:200, MaxBatchInteval:1, MaxBatchTryTimes:3, CreateTime:"2018-01-03T22:25:36.497442704+08:00"}, MetricConfig:[]mgr.MetricConfig(nil), ReaderConfig:conf.MapConf{"mode":"dir", "name":"clusterSalveConfigsTest", "read_from":"oldest", "runner_name":"clusterSalveConfigsTest", "ignore_hidden":"true", "log_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/logdir", "meta_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/meta"}, CleanerConfig:conf.MapConf(nil), ParserConf:conf.MapConf{"name":"json_parser", "runner_name":"clusterSalveConfigsTest", "type":"json"}, Transforms:[]map[string]interface {}(nil), SenderConfig:[]conf.MapConf{conf.MapConf{"file_send_path":"/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/sender/sendData", "name":"file_sender", "runner_name":"clusterSalveConfigsTest", "sender_type":"file"}}, Router:sender.RouterConfig{KeyName:"", MatchType:"", DefaultIndex:0, Routes:map[string]int(nil)}, IsInWebFolder:true, IsStopped:false}}, Tag:"test", Err:error(nil)}}}
	            	
	            	Diff:
	            	--- Expected
	            	+++ Actual
	            	@@ -2,3 +2,3 @@
	            	  Code: (string) (len=4) "L200",
	            	- Data: (map[string]mgr.SlaveConfig) (len=2) {
	            	+ Data: (map[string]mgr.SlaveConfig) (len=1) {
	            	   (string) (len=25) "http://192.168.0.106:6202": (mgr.SlaveConfig) {
	            	@@ -52,52 +52,2 @@
	            	    Err: (error) <nil>
	            	-  },
	            	-  (string) (len=25) "http://192.168.0.106:6203": (mgr.SlaveConfig) {
	            	-   Configs: (map[string]mgr.RunnerConfig) (len=1) {
	            	-    (string) (len=113) "/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/slave2/confs/clusterSalveConfigsTest.conf": (mgr.RunnerConfig) {
	            	-     RunnerInfo: (mgr.RunnerInfo) {
	            	-      RunnerName: (string) (len=23) "clusterSalveConfigsTest",
	            	-      CollectInterval: (int) 1,
	            	-      MaxBatchLen: (int) 1,
	            	-      MaxBatchSize: (int) 200,
	            	-      MaxBatchInteval: (int) 1,
	            	-      MaxBatchTryTimes: (int) 3,
	            	-      CreateTime: (string) (len=35) "2018-01-03T22:25:36.497453622+08:00"
	            	-     },
	            	-     MetricConfig: ([]mgr.MetricConfig) <nil>,
	            	-     ReaderConfig: (conf.MapConf) (len=7) {
	            	-      (string) (len=13) "ignore_hidden": (string) (len=4) "true",
	            	-      (string) (len=8) "log_path": (string) (len=105) "/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/logdir",
	            	-      (string) (len=9) "meta_path": (string) (len=103) "/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/meta",
	            	-      (string) (len=4) "mode": (string) (len=3) "dir",
	            	-      (string) (len=4) "name": (string) (len=23) "clusterSalveConfigsTest",
	            	-      (string) (len=9) "read_from": (string) (len=6) "oldest",
	            	-      (string) (len=11) "runner_name": (string) (len=23) "clusterSalveConfigsTest"
	            	-     },
	            	-     CleanerConfig: (conf.MapConf) <nil>,
	            	-     ParserConf: (conf.MapConf) (len=3) {
	            	-      (string) (len=4) "name": (string) (len=11) "json_parser",
	            	-      (string) (len=11) "runner_name": (string) (len=23) "clusterSalveConfigsTest",
	            	-      (string) (len=4) "type": (string) (len=4) "json"
	            	-     },
	            	-     Transforms: ([]map[string]interface {}) <nil>,
	            	-     SenderConfig: ([]conf.MapConf) (len=1) {
	            	-      (conf.MapConf) (len=4) {
	            	-       (string) (len=14) "file_send_path": (string) (len=114) "/Users/sunjianbo/gopath/src/github.com/qiniu/logkit/mgr/testClusterRest/clusterSalveConfigsTestDir/sender/sendData",
	            	-       (string) (len=4) "name": (string) (len=11) "file_sender",
	            	-       (string) (len=11) "runner_name": (string) (len=23) "clusterSalveConfigsTest",
	            	-       (string) (len=11) "sender_type": (string) (len=4) "file"
	            	-      }
	            	-     },
	            	-     Router: (sender.RouterConfig) {
	            	-      KeyName: (string) "",
	            	-      MatchType: (string) "",
	            	-      DefaultIndex: (int) 0,
	            	-      Routes: (map[string]int) <nil>
	            	-     },
	            	-     IsInWebFolder: (bool) true,
	            	-     IsStopped: (bool) false
	            	-    }
	            	-   },
	            	-   Tag: (string) (len=4) "test",
	            	-   Err: (error) <nil>
	            	   }

Process finished with exit code 1

it means it lost half of data in json map field data

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions