Permalink
Browse files

init

  • Loading branch information...
0 parents commit ca9bbc4708b319c79d99c1db04a1b1d3f4cf2d70 Ryan Smith (ace hacker) committed Mar 25, 2012
Showing with 137 additions and 0 deletions.
  1. +58 −0 lscan.go
  2. +52 −0 lscan_test.go
  3. +27 −0 readme.md
@@ -0,0 +1,58 @@
+package lscan
+
+import (
+ "fmt"
+ "io"
+ "text/scanner"
+)
+
+var s scanner.Scanner
+
+func Parse(r io.Reader) (kvmap map[string]interface{}) {
+ s.Init(r)
+ tok := s.Scan()
+ kvmap = make(map[string]interface{})
+
+ key := ""
+ val := ""
+ ink := true // we always start with the key
+ inv := false
+
+ for tok != scanner.EOF {
+ tex := s.TokenText()
+ s.Whitespace = 0 // don't skip whitespace
+ fmt.Printf("handle=%v ", tex)
+ if tex == `=` {
+ fmt.Printf("split ")
+ ink = false
+ inv = true
+ tok = s.Scan()
+ continue
+ }
+ if tex == ` ` {
+ fmt.Printf("next ")
+ kvmap[key] = val
+ key = ""
+ val = ""
+ ink = true
+ inv = false
+ tok = s.Scan()
+ continue
+ }
+ if ink {
+ key += tex
+ fmt.Printf("key=%v ", key)
+ } else if inv {
+ val += tex
+ fmt.Printf("val=%v ", val)
+ }
+
+ tok = s.Scan()
+ // We are done. :( Let us write whatever we have left
+ // to the map
+ if tok == scanner.EOF {
+ kvmap[key] = val
+ }
+ }
+ return
+}
@@ -0,0 +1,52 @@
+package lscan
+
+import (
+ "strings"
+ "testing"
+)
+
+var str = `hello=world name="ryan\"smith" distance=1.123 time="2012-03-21 10:18:20 -0700"`
+
+func TestParseSimple(t *testing.T) {
+ in := strings.NewReader(str)
+ m := Parse(in)
+
+ actual := m["hello"]
+ expected := "world"
+ if actual != expected {
+ t.Errorf("\n e(%v) \n a(%v)", expected, actual)
+ }
+}
+
+func TestParseQuoted(t *testing.T) {
+ in := strings.NewReader(str)
+ m := Parse(in)
+
+ actual := m["name"]
+ expected := `"ryan\"smith"`
+ if actual != expected {
+ t.Errorf("\n e(%v) \n a(%v)", expected, actual)
+ }
+}
+
+func TestParseNum(t *testing.T) {
+ in := strings.NewReader(str)
+ m := Parse(in)
+
+ actual := m["distance"]
+ expected := `1.123`
+ if actual != expected {
+ t.Errorf("\n e(%v) \n a(%v)", expected, actual)
+ }
+}
+
+func TestParseTime(t *testing.T) {
+ in := strings.NewReader(str)
+ m := Parse(in)
+
+ actual := m["time"]
+ expected := `"2012-03-21 10:18:20 -0700"`
+ if actual != expected {
+ t.Errorf("\n e(%v) \n a(%v)", expected, actual)
+ }
+}
@@ -0,0 +1,27 @@
+# lscan
+
+## Synopsis
+
+Turn k=v log data into Go maps.
+
+## Description
+
+This pkg is used in wcld to parse log lines into maps to store in a PostgreSQL
+database. See the test file for examples.
+
+## Build
+
+First you will need the latest Go1 RC.
+
+```bash
+$ cd $GOROOT
+$ hg pull
+$ hg update weekly
+$ ./src/all.bash
+```
+
+After that you are all set!
+
+```bash
+$ go test .
+```

0 comments on commit ca9bbc4

Please sign in to comment.