Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
jsonImporter fixups
Browse files Browse the repository at this point in the history
1) Tolerate lists with no entries
2) Exit with error messages instead of Chk.NoError() in main()
3) General variable names
4) Use json.NewDecoder() instead of reading all data into memory again.
5) Idiomatic fixes.

Addresses issue #20
  • Loading branch information
cmasone-attic committed Jul 9, 2015
1 parent 1faabb2 commit 7b91f06
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions clients/jsonImporter/jsonImporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,44 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/attic-labs/noms/chunks"
"github.com/attic-labs/noms/datas"
"github.com/attic-labs/noms/dbg"
"github.com/attic-labs/noms/types"
)

func nomsValueFromObject(o interface{}) types.Value {
switch oo := o.(type) {
switch o := o.(type) {
case string:
return types.NewString(oo)
return types.NewString(o)
case bool:
return types.Bool(oo)
return types.Bool(o)
case float64:
return types.Float64(oo)
return types.Float64(o)
case nil:
return nil
case []interface{}:
out := types.NewList()
for _, v := range oo {
out = out.Append(nomsValueFromObject(v))
for _, v := range o {
nv := nomsValueFromObject(v)
if nv != nil {
out = out.Append(nv)
}
}
return out
case map[string]interface{}:
out := types.NewMap()
for k, v := range oo {
for k, v := range o {
nv := nomsValueFromObject(v)
if nv != nil {
out = out.Set(types.NewString(k), nv)
}
}
return out
default:
fmt.Println(oo, "is of a type I don't know how to handle")
fmt.Println(o, "is of a type I don't know how to handle")
}
return nil
}
Expand All @@ -60,24 +62,27 @@ func main() {
}

res, err := http.Get(url)
dbg.Chk.NoError(err)

gameEventsJSON, err := ioutil.ReadAll(res.Body)
res.Body.Close()
dbg.Chk.NoError(err)
defer res.Body.Close()
if err != nil {
log.Fatalf("Error fetching %s: %+v\n", url, err)
} else if res.StatusCode != 200 {
log.Fatalf("Error fetching %s: %s\n", url, res.Status)
}

var events interface{}
err = json.Unmarshal(gameEventsJSON, &events)
dbg.Chk.NoError(err)
var jsonObject interface{}
err = json.NewDecoder(res.Body).Decode(&jsonObject)
if err != nil {
log.Fatalln("Error decoding JSON: ", err)
}

ds := datas.NewDataStore(cs)
roots := ds.Roots()

gameEvents := nomsValueFromObject(events)
value := nomsValueFromObject(jsonObject)

ds.Commit(datas.NewRootSet().Insert(
datas.NewRoot().SetParents(
roots.NomsValue()).SetValue(
gameEvents)))
value)))

}

0 comments on commit 7b91f06

Please sign in to comment.