package main; import ( "io"; "os"; "fmt"; "time"; "strings"; "reflect"; "net/url"; "net/http"; "encoding/json"; "github.com/xaionaro/gorm"; _ "github.com/mattn/go-sqlite3"; models "devel.mephi.ru/ut/download-publications-models" ); type Config struct { DBPath string; PublicationsURL string; AuthorshipsURL string; }; func stripChars(str, chr string) string { // The function is from here: https://www.rosettacode.org/wiki/Strip_a_set_of_characters_from_a_string#Go return strings.Map(func(r rune) rune { if strings.IndexRune(chr, r) < 0 { return r } return -1 }, str) } func getJson(urlStr string) (r []map[string]interface{}, err error) { var reader io.Reader; u, e := url.Parse(urlStr); if (e == nil && u.Scheme != "") { resp, err := http.Get(urlStr); if (err != nil) { return r, err; } defer resp.Body.Close(); reader = resp.Body; } else { file, err := os.Open(urlStr); if (err != nil) { return r, err; } defer file.Close(); reader = file; } decoder := json.NewDecoder(reader); decoder.UseNumber(); decoder.Decode(&r); return r, nil; } func getConfig() (config Config) { file, err := os.Open("config.json"); if (err != nil) { fmt.Errorf("Config file error: %v", err.Error()); os.Exit(-1); } defer file.Close(); decoder := json.NewDecoder(file); decoder.Decode(&config); //fmt.Printf("config: %v\n", config); return config; } func parseValue(in interface{}) (out interface{}) { switch v := in.(type) { case json.Number: i,_ := v.Int64(); return int(i); case string: t,e := time.Parse("2006-01-02T15:04:05.000-07:00", v); //????-??-??T??:??:??.???+??:?? if (e == nil) { return t; } break; } return in; } // TODO: make this function recursive func parse(datasValueMap []map[string]interface{}, dataSample interface{}) (interface{}) { dataFieldMap := make(map[string]string); dataValue := reflect.ValueOf(dataSample).Elem(); dataType := dataValue.Type(); //datas := reflect.MakeSlice(reflect.SliceOf(dataType), 0, 0) var datas []interface{} // Getting fields names and making a map of ToLower(Name) -> Name fields := dataValue.NumField(); for i:=0; i