Search Gear #10 – I need your critical comments!

Go, I need your constructive critical comments.

For me constructive criticism is the fast way to learn. So here is my first simple bit of code (which I’m prepared to share).

How would you have done it differently and why is your way better than mine? What’s wrong with my Go style, and what bits are not go-idiomatic? – see it better at http://play.golang.org/p/9tOXSFUHT2

package configure

import (
 "github.com/laurent22/toml-go"
 "errors"
 "io/ioutil"
)
type twitter struct {
 apiKey string
 apiSecret string
 userKey string
 userSecret string
 users toml.Value
 tags toml.Value
 errors error
}

func lang(this string) string {
 /* this is currently a placeholder. It just returns the string it's given.
 Eventually, However it will look for that string a those pulled from a language file 
 based on the config language variable. Not elegant but the others I've looked at:
 http://stackoverflow.com/questions/14124630/i18n-strategies-for-go-with-app-engine
 gettext: a MO file parser
 go-18n
 Internationalization plan for Go
 Polyglot
 Aren't doing it for me*/
 
 return this
}

func (this *twitter) Configure (file string) (*twitter, error) {
 
 var parser toml.Parser
 var clear toml.Value
 
 if _, err := ioutil.ReadFile(file); err != nil { //surely resetting the struct can't be this torturous?
 this.userKey = ""
 this.userSecret = ""
 this.apiKey = ""
 this.apiSecret = ""
 this.users = clear // return to this - surely this can't be the way to clear this variable.
 this.tags = clear // return to this - surely this can't be the way to clear this variable.
 this.errors = errors.New(lang("Could not find the Configuration File. You'll need to provide these setting for twitter to work."))
 }else{
 toml := parser.ParseFile(file)
 
 this.userKey = toml.GetString("twitter.user-key")
 this.userSecret = toml.GetString("twitter.user-secret")
 this.apiKey = toml.GetString("twitter.api-key", "26276391-gtihvXIuNXzCD4HTOjx8630O8IPq19m810TxkLMmd")
 this.apiSecret = toml.GetString("twitter.api-secret", "bYkulR93l70elsjZysjjDpxQT69RVF2GIUcjoMIkrADoc")
 this.users, _ = toml.GetValue("twitter.specific-users")
 this.tags, _ = toml.GetValue("twitter.specific-tags")
 this.errors = nil
 }
 return this, this.errors
}

Why is this so short? Well I’d like to not be writing lots of bad code and then asking for comment! Code at http://play.golang.org/p/qEA9hXgEIT)

 

TESTS
 package configure

import (
 "testing" 
)

/*
go test -v
go test -cover
go test -coverprofile=coverage.out 
go tool cover -func=cover.out
go tool cover -html=coverage.out
*/

func TestLang(t *testing.T) {
 // test structure grabbed from https://talks.golang.org/2014/testing.slide#5 and modified accordingly.
 var tests = []struct {
 given string
 want string
 }{
 {"test Strings", "test Strings"},
 }
 
 for _, test := range tests {
 actual := lang(test.given)
 if actual != test.want {
 t.Errorf("Test Failed: Given '%v'; Expected '%v'; Got '%v'", test.given, test.want, actual)
 }
 }
}

func TestConfigure(t *testing.T) {

 twitterTest := new(twitter)
 
 // test structure grabbed from https://talks.golang.org/2014/testing.slide#5 and modified accordingly.
 var tests = []struct {
 given string
 want string
 shouldError bool
 }{
 {"", "", true},
 {"myconfig.toml", "26276391-gtihvXIuNXzCD4HTOjx8630O8IPq19m810TxkLMmd", false},
 {"", "", true},
 }
 
 for iCount, test := range tests {
 if actual, errors := twitterTest.Configure(test.given); errors == nil {
 if test.shouldError == true { // No error occured but errors expected
 t.Errorf("Test #%v Failed: 'Error Expected', but got 'No Error'", iCount)
 } else {
 if actual.apiKey != test.want {
 t.Errorf("Test Failed: Given '%v'; Wanted '%v'; Got '%v'", test.given, test.want, actual.apiKey)
 } 
 }
 } else { 
 if test.shouldError == false { // Error occured but No errors expected
 t.Errorf("Test #%v Failed: 'Error Occurred', but was 'Not Expected'", iCount)
 }
 }
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s