/
main.go
74 lines (57 loc) · 1.62 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
72
73
74
// Package native provides generator for native odata client
package native
import (
"fmt"
"github.com/SysUtils/1c-gateway/shared"
"log"
"os"
)
type Generator struct {
TypeMap map[string]string
NameMap map[string]string
Associations map[string]map[string]string
schema shared.Schema
}
// NewGenerator returns initialized generator
func NewGenerator(schema shared.Schema) *Generator {
return &Generator{schema: schema, TypeMap: make(map[string]string), NameMap: make(map[string]string), Associations: make(map[string]map[string]string)}
}
// Generate generates the native odata client and writes it to ./odata folder
func (g *Generator) Generate() {
data := fmt.Sprintf(`package odata
import (
"encoding/json"
"errors"
)
%s`, g.genTypes(g.schema.Entities))
g.writeGofile("Entity.go", data)
data = fmt.Sprintf(`package odata
%s`, g.genPrimaryKeys(g.schema.Entities))
g.writeGofile("Primary.go", data)
data = fmt.Sprintf(`package odata
%s`, g.genComplexTypes(g.schema.Complexes))
g.writeGofile("Complex.go", data)
data = fmt.Sprintf(`package odata
%s`, g.genFunctions(g.schema.Functions))
g.writeGofile("Functions.go", data)
data = fmt.Sprintf(`package odata
%s`, g.genNavigations(g.schema.Entities))
g.writeGofile("Navigations.go", data)
data = fmt.Sprintf(`package odata
%s`, g.genFilters(g.schema.Entities))
g.writeGofile("Filters.go", data)
}
func (g *Generator) writeGofile(filename, data string) {
f, err := os.Create("odata/" + filename)
if err != nil {
log.Panic(err)
}
_, err = f.WriteString(data)
if err != nil {
log.Panic(err)
}
err = f.Close()
if err != nil {
log.Panic(err)
}
}