matryer/goblueprints

Bighugh.go fails on words without Verb Synonyms

OAGr opened this issue · 2 comments

OAGr commented

I imagine that we need to just check if there is data in data.Verb.Syn before accessing it, but I am not sure what the cleanest way of doing that is.

bighugh.go

type words struct {
    Syn []string `json:"syn"`
}
...
syns = append(syns, data.Verb.Syn...)

curl attempt

curl http://words.bighugelabs.com/api/2/f67148bb1412bef4c1c6b25f6947e8f6/woman/json                                |
{"noun":{"syn":["adult female","charwoman","char","cleaning woman","cleaning lady","womanhood","fair sex","adult","class","cleaner","female","female person","grownup","social class","socio-economic class"],"ant":["man"],"usr":["girl"]}}%

code attempt

echo 'woman' | ./synonyms                                                                                          |
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x463652]

goroutine 1 [running]:
thesaurus.(*BigHugh).Synonyms(0xc20808bec0, 0xc20800a440, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/ozzie/src/thesaurus/bighugh.go:36 +0x942
main.main()
        /home/ozzie/src/github.com/oagr/blueprints/synonyms/main.go:17 +0x2a9

goroutine 8 [runnable]:
net/http.(*persistConn).readLoop(0xc20806c000)
        /usr/lib/go/src/net/http/transport.go:928 +0x9ce
created by net/http.(*Transport).dialConn
        /usr/lib/go/src/net/http/transport.go:660 +0xc9f

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 9 [select]:
net/http.(*persistConn).writeLoop(0xc20806c000)
        /usr/lib/go/src/net/http/transport.go:945 +0x41d
created by net/http.(*Transport).dialConn
        /usr/lib/go/src/net/http/transport.go:661 +0xcbc

You're right, that little block should probably be a little more forgiving:

if data.Noun != nil {
    syns = append(syns, data.Noun.Syn...)
}
if data.Verb != nil {
    syns = append(syns, data.Verb.Syn...)
}

Thanks.

Note, that calling append(syns, data.Verb.Syn...) is OK if Syn is an empty slice. It just won't do anything. The problem is that data.Verb is nil.