package main import ( "encoding/json" "fmt" "net" "os" "qurl/qurl" "qurl/storage" "time" ) type qurljson struct { ID uint64 `json:"id"` IP string `json:"ip"` Browser string `json:"browser"` URL string `json:"url"` Date struct { Date int64 `json:"$date"` } `json:"date"` } func loadjson(stor storage.Storage, filename string) error { file, err := os.Open(filename) if err != nil { return fmt.Errorf("Error opening %s: %s", filename, err.Error()) } defer file.Close() var qj []qurljson decoder := json.NewDecoder(file) err = decoder.Decode(&qj) if err != nil { return fmt.Errorf("Error parsing %s: %s", filename, err.Error()) } fmt.Printf("Parsing %d qurls.. \n", len(qj)) var max uint64 = 0 var count uint64 = 0 for _, e := range qj { if e.ID > max { max = e.ID } q := &qurl.QURL{ ID: e.ID, URL: e.URL, } if e.Date.Date > 0 { q.Created = time.Unix((e.Date.Date / 1000), 0) } if e.IP != "" { q.IP = net.ParseIP(e.IP) } if e.Browser != "" { q.Browser = e.Browser } err := q.CheckValid() if err != nil { fmt.Printf("\n%s\nValidation failure: %s\n", q.URL, err.Error()) continue } err = stor.AddQURL(q) if err != nil { return fmt.Errorf("AddQURL() Database error: %s", err.Error()) } count++ if (count % 100) == 0 { fmt.Printf("*") } } err = stor.SetQURLSequence(max) if err != nil { return fmt.Errorf("Error setting sequence: %s", err.Error()) } fmt.Printf("\nDone!\n") return nil }