Skip to content

Commit

Permalink
Merge pull request name5566#15 from ljfuyuan/mapsupport
Browse files Browse the repository at this point in the history
add map support
  • Loading branch information
name5566 authored Jun 23, 2016
2 parents f1788f9 + eee43ae commit a870d54
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 69 deletions.
64 changes: 0 additions & 64 deletions recordfile/example_test.go

This file was deleted.

73 changes: 73 additions & 0 deletions recordfile/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package recordfile

import (
"testing"
)

type Mysql struct {
Host, Port, User, Passwd, Db string
}

func Test_map(t *testing.T){
type Record struct {
// index 0
IndexInt int "index"
// index 1
IndexStr string "index"
_Number int32
Str string
Arr1 [2]int
Arr2 [3][2]int
Arr3 []int
St struct {
Name string "name"
Num int "num"
}
Maps map[string]Mysql
}

rf, err := New(Record{})
if err != nil {
t.Error(err)
}

err = rf.Read("test.txt")
if err != nil {
t.Error(err)
}

for i := 0; i < rf.NumRecord(); i++ {
r := rf.Record(i).(*Record)
t.Log(r.IndexInt)
}

r := rf.Index(2).(*Record)
t.Log(r.Str)

r = rf.Indexes(0)[2].(*Record)
t.Log(r.Str)

r = rf.Indexes(1)["three"].(*Record)
t.Log(r.Str)
t.Log(r.Arr1[1])
t.Log(r.Arr2[2][0])
t.Log(r.Arr3[0])
t.Log(r.St.Name)
t.Log(r.Maps)

//go test -v .
//=== RUN Test_map
//--- PASS: Test_map (0.00s)
//map_test.go:41: 1
//map_test.go:41: 2
//map_test.go:41: 3
//map_test.go:45: cat
//map_test.go:48: cat
//map_test.go:51: book
//map_test.go:52: 6
//map_test.go:53: 4
//map_test.go:54: 6
//map_test.go:55: name5566
//map_test.go:56: map[Mysql1:{127.0.0.1 3306 root db1} Mysql2:{127.0.0.2 3306 root ccc db2}]

}
4 changes: 3 additions & 1 deletion recordfile/recordfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func New(st interface{}) (*RecordFile, error) {
case reflect.Struct:
case reflect.Array:
case reflect.Slice:
case reflect.Map:
default:
return nil, fmt.Errorf("invalid type: %v %s",
f.Name, kind)
Expand Down Expand Up @@ -170,7 +171,8 @@ func (rf *RecordFile) Read(name string) error {
field.SetString(strField)
} else if kind == reflect.Struct ||
kind == reflect.Array ||
kind == reflect.Slice {
kind == reflect.Slice ||
kind == reflect.Map{
err = json.Unmarshal([]byte(strField), field.Addr().Interface())
}

Expand Down
8 changes: 4 additions & 4 deletions recordfile/test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
数字索引 字符串索引 数字类型 字符串类型 数组类型 嵌套数组 变长数组 结构体类型
1 one 0 knife "[1, 2]" "[[0,1], [1,2], [2,3]]" "[1, 2, 3]" "{""name"": ""name5566"", ""num"": 1}"
2 two 0 cat "[3, 4]" "[[1,2], [2,3], [3,4]]" "[4, 5]" "{""name"": ""name5566"", ""num"": 2}"
3 three 0 book "[5, 6]" "[[2,3], [3,4], [4,5]]" [6] "{""name"": ""name5566"", ""num"": 3}"
数字索引 字符串索引 数字类型 字符串类型 数组类型 嵌套数组 变长数组 结构体类型 Map类型
1 one 0 knife "[1, 2]" "[[0,1], [1,2], [2,3]]" "[1, 2, 3]" "{""name"": ""name5566"", ""num"": 1}" "{""Mysql1"": {""Host"":""127.0.0.1"",""Port"":""3306"",""User"":""root"",""Passwd"":""111"",""Db"":""db1""},""Mysql2"": {""Host"":""127.0.0.2"",""Port"":""3306"",""User"":""root"",""Passwd"":""aaa"",""Db"":""db2""}}"
2 two 0 cat "[3, 4]" "[[1,2], [2,3], [3,4]]" "[4, 5]" "{""name"": ""name5566"", ""num"": 2}" "{""Mysql1"": {""Host"":""127.0.0.1"",""Port"":""3306"",""User"":""root"",""Passwd"":"""",""Db"":""db1""},""Mysql2"": {""Host"":""127.0.0.2"",""Port"":""3306"",""User"":""root"",""Passwd"":""bbb"",""Db"":""db2""}}"
3 three 0 book "[5, 6]" "[[2,3], [3,4], [4,5]]" [6] "{""name"": ""name5566"", ""num"": 3}" "{""Mysql1"": {""Host"":""127.0.0.1"",""Port"":""3306"",""User"":""root"",""Passwd"":"""",""Db"":""db1""},""Mysql2"": {""Host"":""127.0.0.2"",""Port"":""3306"",""User"":""root"",""Passwd"":""ccc"",""Db"":""db2""}}"

0 comments on commit a870d54

Please sign in to comment.