move: log parsers
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// Remember: This should point to the directory exactly after GOPATH
|
||||
// Use / not \ even on Windows
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// TODO: funcs can call other funcs
|
||||
|
||||
const data = `New York,100,2,1,100000
|
||||
Hong Kong,150,3,2,300000
|
||||
Paris,200,4,3,250000
|
||||
Istanbul,500,10,5,four hundred thousand`
|
||||
|
||||
props, err := parse(data)
|
||||
if err != nil {
|
||||
fmt.Printf("> err: %s\n\n", err)
|
||||
}
|
||||
|
||||
show(props)
|
||||
|
||||
// fmt.Printf("%#v\n", props.list)
|
||||
}
|
@@ -1,84 +0,0 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const separator = ","
|
||||
|
||||
// Parse parses the given data and returns a slice of Properties
|
||||
func parse(data string) (props []property, err error) {
|
||||
rows := strings.Split(data, "\n")
|
||||
|
||||
for _, row := range rows {
|
||||
cols := strings.Split(row, separator)
|
||||
|
||||
loc := cols[0]
|
||||
|
||||
// size, err := strconv.Atoi(cols[1])
|
||||
// if err != nil {
|
||||
// return props, err
|
||||
// }
|
||||
|
||||
// beds, err := strconv.Atoi(cols[2])
|
||||
// if err != nil {
|
||||
// return props, err
|
||||
// }
|
||||
|
||||
// baths, err := strconv.Atoi(cols[3])
|
||||
// if err != nil {
|
||||
// return props, err
|
||||
// }
|
||||
|
||||
// price, err := strconv.Atoi(cols[4])
|
||||
// if err != nil {
|
||||
// return props, err
|
||||
// }
|
||||
|
||||
size, err := atoi(cols[1], err)
|
||||
beds, err := atoi(cols[2], err)
|
||||
baths, err := atoi(cols[3], err)
|
||||
price, err := atoi(cols[4], err)
|
||||
|
||||
if err != nil {
|
||||
return props, err
|
||||
}
|
||||
|
||||
prop := property{
|
||||
location: loc,
|
||||
size: size,
|
||||
beds: beds,
|
||||
baths: baths,
|
||||
price: price,
|
||||
}
|
||||
|
||||
props = append(props, prop)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// atoi is a helper for strconv.Atoi
|
||||
// it saves the previous error to simplify the error handling.
|
||||
// usage:
|
||||
// n, err := atoi(p, err)
|
||||
// m, err := atoi(q, err)
|
||||
func atoi(s string, err error) (int, error) {
|
||||
// if there was an error return it instead: skip the Atoi
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
n, lerr := strconv.Atoi(s)
|
||||
if lerr != nil {
|
||||
return 0, lerr
|
||||
}
|
||||
return n, nil
|
||||
}
|
@@ -1,65 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
const data = `New York,100,2,1,100000
|
||||
Istanbul,150,3,2,200000`
|
||||
|
||||
props := parse(data)
|
||||
|
||||
if l := len(props); l != 2 {
|
||||
t.Fatalf(`got: %d; want: 2`, l)
|
||||
}
|
||||
|
||||
want := Property{
|
||||
Location: "New York",
|
||||
Size: 100, Beds: 2, Baths: 1, Price: 100000,
|
||||
}
|
||||
p := props[0]
|
||||
|
||||
if !reflect.DeepEqual(want, p) {
|
||||
t.Errorf(`got: %#v; want: %#v`, p, want)
|
||||
}
|
||||
|
||||
want = Property{
|
||||
Location: "Istanbul",
|
||||
Size: 150, Beds: 3, Baths: 2, Price: 200000,
|
||||
}
|
||||
|
||||
p = props[1]
|
||||
if !reflect.DeepEqual(want, p) {
|
||||
t.Errorf(`got: %#v; want: %#v`, p, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseBetter(t *testing.T) {
|
||||
const data = `New York,100,2,1,100000
|
||||
Istanbul,150,3,2,200000`
|
||||
|
||||
tests := []Property{
|
||||
{
|
||||
Location: "New York",
|
||||
Size: 100, Beds: 2, Baths: 1, Price: 100000,
|
||||
},
|
||||
{
|
||||
Location: "Istanbul",
|
||||
Size: 150, Beds: 3, Baths: 2, Price: 200000,
|
||||
},
|
||||
}
|
||||
|
||||
props := parse(data)
|
||||
|
||||
if l := len(props); l != 2 {
|
||||
t.Fatalf(`got: %d; want: 2`, l)
|
||||
}
|
||||
|
||||
for i, p := range tests {
|
||||
if !reflect.DeepEqual(p, props[i]) {
|
||||
t.Errorf(`got: %#v; want: %#v`, props[i], p)
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,37 +0,0 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// show shows the given properties
|
||||
func show(props []property) {
|
||||
showHeader()
|
||||
|
||||
for _, p := range props {
|
||||
fmt.Printf("%-15s", p.location)
|
||||
fmt.Printf("%-15d", p.size)
|
||||
fmt.Printf("%-15d", p.beds)
|
||||
fmt.Printf("%-15d", p.baths)
|
||||
fmt.Printf("%-15d", p.price)
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
|
||||
// showHeader prints the header
|
||||
func showHeader() {
|
||||
const header = "Location,Size,Beds,Baths,Price"
|
||||
|
||||
for _, h := range strings.Split(header, separator) {
|
||||
fmt.Printf("%-15s", h)
|
||||
}
|
||||
fmt.Printf("\n%s\n", strings.Repeat("=", 75))
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// property stores data about a property
|
||||
type property struct {
|
||||
location string
|
||||
size, beds, baths, price int
|
||||
}
|
@@ -145,6 +145,6 @@ func main() {
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile("replay.log", inputs, 0644); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Cannot save replay: ", err)
|
||||
fmt.Fprintf(os.Stderr, "Cannot save replay: %v", err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user