-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (57 loc) · 1.77 KB
/
main.go
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
package main
import (
"fmt"
"reflect"
"strings"
"github.com/TheOpenDictionary/odict/lib/core"
"github.com/iancoleman/strcase"
)
type Identifiable struct {
ID string
}
func Check(err error) {
if err != nil {
panic(err)
}
}
func createQuery(s interface{}) string {
val := reflect.ValueOf(s)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return ""
}
tableName := strcase.ToLowerCamel(strings.Replace(val.Type().Name(), "Representable", "", -1))
columns := []string{}
values := []string{}
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldName := strcase.ToLowerCamel(val.Type().Field(i).Name)
if field.Kind() == reflect.Slice && field.Type().Elem().Kind() == reflect.Struct {
for j := 0; j < field.Len(); j++ {
subInsert := createQuery(field.Index(j).Interface())
columns = append(columns, fieldName)
values = append(values, fmt.Sprintf("(%s).id", subInsert))
}
} else if field.Kind() == reflect.Map && field.Type().Elem().Kind() == reflect.Struct {
iter := field.MapRange()
for iter.Next() {
subInsert := createQuery(iter.Value().Interface())
columns = append(columns, fieldName)
values = append(values, fmt.Sprintf("(%s).id", subInsert))
}
} else if fieldName != "id" && fieldName != "xmlname" {
columns = append(columns, fieldName)
values = append(values, fmt.Sprintf("'%v'", field.Interface()))
}
}
return fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) RETURN id", tableName, strings.Join(columns, ", "), strings.Join(values, ", "))
}
func main() {
dict, _ := core.ReadDictionaryFromPath("/Users/tjnickerson/.linguistic/dictionaries/eng-eng.odict")
d := dict.AsRepresentable()
print(createQuery(d.Entries["create"]))
// writeData(db, d)
fmt.Printf("All done!")
}