Skip to content

Commit

Permalink
fix: if category is missing, don't return an error
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffrey Bauduin <[email protected]>
  • Loading branch information
geoffreybauduin committed Jan 15, 2020
1 parent bc29f9e commit e39e043
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/geoffreybauduin/redis-info

go 1.13

require (
github.com/maxatome/go-testdeep v1.1.2
github.com/stretchr/testify v1.4.0
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/maxatome/go-testdeep v1.1.2 h1:pN/Xd8hSaH5QKND0JJf51H5kG5Mje4JCus6UKLLkbeA=
github.com/maxatome/go-testdeep v1.1.2/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
10 changes: 7 additions & 3 deletions redisinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ func (i *Info) fromString(content string) error {
for idx := 0; idx < val.NumField(); idx++ {
field := val.Type().Field(idx)
if categoryName := field.Tag.Get("json"); categoryName != "-" && categoryName != "" {
if contentPerCategory[categoryName] == nil || len(contentPerCategory[categoryName]) == 0 {
// empty category
continue
}
fieldVal := val.Field(idx)
fieldValue := reflect.New(fieldVal.Type())
if categoryValue, ok := fieldValue.Interface().(category); ok {
if err := categoryValue.fromString(strings.Join(contentPerCategory[categoryName], "\n")); err != nil {
return err
return fmt.Errorf("cannot parse category %s: %w", categoryName, err)
}
valueToSet := reflect.Indirect(reflect.ValueOf(categoryValue))
if !fieldVal.CanSet() {
Expand Down Expand Up @@ -226,10 +230,10 @@ func (s *Keyspace) fromString(content string) error { return parseStruct(s, cont
func sep(content string, lineSep string, keySep string) (map[string]string, error) {
parsed := map[string]string{}
lines := strings.Split(content, lineSep)
for _, line := range lines {
for nbr, line := range lines {
lineContent := strings.Split(line, keySep)
if len(lineContent) != 2 {
return nil, fmt.Errorf("invalid line: %s", line)
return nil, fmt.Errorf("invalid line %d: '%s'", nbr, line)
}
parsed[lineContent[0]] = lineContent[1]
}
Expand Down
31 changes: 30 additions & 1 deletion redisinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)

func Test1(t *testing.T) {
func Test_Full(t *testing.T) {
content, err := ioutil.ReadFile("./resources/output.txt")
assert.NoError(t, err)
info, err := redisinfo.Parse(string(content))
Expand Down Expand Up @@ -191,3 +191,32 @@ func Test1(t *testing.T) {
},
}, td.StructFields{}, "got correct structure")
}

func Test_OnlyServer(t *testing.T) {
content, err := ioutil.ReadFile("./resources/output-server.txt")
assert.NoError(t, err)
info, err := redisinfo.Parse(string(content))
assert.NoError(t, err)
td.CmpStruct(t, info, redisinfo.Info{
Server: redisinfo.Server{
RedisVersion: "3.2.5",
RedisGitSha1: "00000000",
RedisGitDirty: false,
RedisBuildID: "6f9920d2ae584aa0",
RedisMode: "standalone",
OS: "Linux 4.14.66-ovh-vps-grsec-zfs-classid x86_64",
ArchBits: 64,
MultiplexingAPI: "epoll",
GCCVersion: "4.9.2",
ProcessID: 7,
RunID: "a4fcd0061e667352b73ad678f944d41b23c9c67a",
TCPPort: 6379,
UptimeInDays: 24,
UptimeInSeconds: 2089968,
HZ: 10,
LRUClock: 1870516,
Executable: "/data/redis-server",
ConfigFile: "/redis.conf",
},
}, td.StructFields{}, "got correct structure")
}
19 changes: 19 additions & 0 deletions resources/output-server.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Server
redis_version:3.2.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:6f9920d2ae584aa0
redis_mode:standalone
os:Linux 4.14.66-ovh-vps-grsec-zfs-classid x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.9.2
process_id:7
run_id:a4fcd0061e667352b73ad678f944d41b23c9c67a
tcp_port:6379
uptime_in_seconds:2089968
uptime_in_days:24
hz:10
lru_clock:1870516
executable:/data/redis-server
config_file:/redis.conf

0 comments on commit e39e043

Please sign in to comment.