Added prechecking script, fixed lint errors

This commit is contained in:
mfarah 2015-09-28 11:58:56 +10:00
parent 9869a41122
commit 0c5188acb5
3 changed files with 31 additions and 30 deletions

3
precheckin.sh Executable file
View File

@ -0,0 +1,3 @@
gofmt -w .
golint
go test

44
yaml.go
View File

@ -20,55 +20,54 @@ func main() {
Name: "read",
Aliases: []string{"r"},
Usage: "read <filename> <path>\n\te.g.: yaml read sample.json a.b.c\n\t(default) reads a property from a given yaml file",
Action: read_property,
Action: readProperty,
},
}
app.Action = read_property
app.Action = readProperty
app.Run(os.Args)
}
func read_property(c *cli.Context) {
var parsed_data map[interface{}]interface{}
read_yaml(c, &parsed_data)
func readProperty(c *cli.Context) {
var parsedData map[interface{}]interface{}
readYaml(c, &parsedData)
var path = c.Args()[1]
var paths = strings.Split(path, ".")
fmt.Println(read_map(parsed_data, paths[0], paths[1:len(paths)]))
fmt.Println(readMap(parsedData, paths[0], paths[1:len(paths)]))
}
func read_yaml(c *cli.Context, parsed_data *map[interface{}]interface{}) {
func readYaml(c *cli.Context, parsedData *map[interface{}]interface{}) {
if len(c.Args()) == 0 {
log.Fatalf("Must provide filename")
}
var raw_data = read_file(c.Args()[0])
var rawData = readFile(c.Args()[0])
if len(c.Args()) == 1 {
fmt.Println(string(raw_data[:]))
fmt.Println(string(rawData[:]))
os.Exit(0)
}
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
err := yaml.Unmarshal([]byte(rawData), &parsedData)
if err != nil {
log.Fatalf("error: %v", err)
}
}
func read_file(filename string) []byte {
var raw_data, read_error = ioutil.ReadFile(filename)
if read_error != nil {
log.Fatalf("error: %v", read_error)
func readFile(filename string) []byte {
var rawData, readError = ioutil.ReadFile(filename)
if readError != nil {
log.Fatalf("error: %v", readError)
}
return raw_data
return rawData
}
func read_map(context map[interface{}]interface{}, head string, tail []string) interface{} {
func readMap(context map[interface{}]interface{}, head string, tail []string) interface{} {
value := context[head]
if len(tail) > 0 {
return recurse(value, tail[0], tail[1:len(tail)])
} else {
return value
}
return value
}
func recurse(value interface{}, head string, tail []string) interface{} {
@ -78,17 +77,16 @@ func recurse(value interface{}, head string, tail []string) interface{} {
if err != nil {
log.Fatalf("Error accessing array: %v", err)
}
return read_array(value.([]interface{}), index, tail)
return readArray(value.([]interface{}), index, tail)
default:
return read_map(value.(map[interface{}]interface{}), head, tail)
return readMap(value.(map[interface{}]interface{}), head, tail)
}
}
func read_array(array []interface{}, head int64, tail []string) interface{} {
func readArray(array []interface{}, head int64, tail []string) interface{} {
value := array[head]
if len(tail) > 0 {
return recurse(value, tail[0], tail[1:len(tail)])
} else {
return value
}
return value
}

View File

@ -7,17 +7,17 @@ import (
"testing"
)
var raw_data = `
var rawData = `
a: Easy!
b:
c: 2
d: [3, 4]
`
var parsed_data map[interface{}]interface{}
var parsedData map[interface{}]interface{}
func TestMain(m *testing.M) {
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
err := yaml.Unmarshal([]byte(rawData), &parsedData)
if err != nil {
fmt.Println("Error parsing yaml: %v", err)
os.Exit(1)
@ -26,15 +26,15 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func TestRead_map_simple(t *testing.T) {
result := read_map(parsed_data, "b", []string{"c"})
func TestReadMap_simple(t *testing.T) {
result := readMap(parsedData, "b", []string{"c"})
if result != 2 {
t.Error("Excpted 2 but got ", result)
}
}
func TestRead_map_array(t *testing.T) {
result := read_map(parsed_data, "b", []string{"d", "1"})
func TestReadMap_array(t *testing.T) {
result := readMap(parsedData, "b", []string{"d", "1"})
if result != 4 {
t.Error("Excpted 4 but got ", result)
}