-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.txt
82 lines (67 loc) · 1.95 KB
/
template.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package ngmeasurements
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/kisielk/sqlstruct"
"gopkg.in/mgo.v2"
"log"
"opencoredata.org/ocdJanus/utils"
)
type {{.Structname}}cVSW struct {
Tables []{{.Structname}}table `json:"tables"`
}
type {{.Structname}}table struct {
URL string `json:"url"`
Row []{{.Structname}}janusRow `json:"row"`
}
type {{.Structname}}janusRow struct {
URL string `json:"url"`
Rownum int `json:"rownum"`
Describes []{{.Structname}} `json:"describes"`
}
// make name generic How to load the body of struct
type {{.Structname}} struct {
{{.Structdata}}
}
func {{.Structname}}Model() *{{.Structname}} {
return &{{.Structname}}{}
}
// func JSONData(qry string, uri string, filename string) []byte {
func {{.Structname}}Func(qry string, uri string, filename string, database string, collection string, conn *sql.DB, session *mgo.Session) error {
rows, err := conn.Query(qry)
if err != nil {
log.Printf(`Error with "%s": %s`, qry, err)
}
allResults := []{{.Structname}}janusRow{}
i := 1
for rows.Next() {
d := []{{.Structname}}{}
var t {{.Structname}}
err := sqlstruct.Scan(&t, rows)
if err != nil {
log.Print(err)
}
d = append(d, t)
rowURL := fmt.Sprintf("%s/%s#row=%v", uri, filename, i)
aRow := {{.Structname}}janusRow{rowURL, i, d}
allResults = append(allResults, aRow)
i = i + 1
}
theTable := {{.Structname}}table{fmt.Sprintf("%s/%s", uri, filename), allResults}
tableSet := []{{.Structname}}table{}
tableSet = append(tableSet, theTable)
final := {{.Structname}}cVSW{tableSet}
// Optional. Switch the session to a Strong behavior.
session.SetMode(mgo.Strong, true)
c := session.DB(database).C(collection)
err = c.Insert(&final)
if err != nil {
log.Printf("Janus func Error %v with %s\n", err, filename)
}
log.Printf("File: %s written", filename)
jm, _ := json.MarshalIndent(final, "", " ")
_ = utils.WriteFile(filename, jm)
// session.Close()
return nil
}