-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgenerate.go
127 lines (102 loc) · 2.79 KB
/
generate.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"go/format"
"os"
log "github.com/Sirupsen/logrus"
"github.com/contiv/modelgen/generators"
)
var validPropertyTypes = []string{
"string",
"bool",
"array",
"number",
"int",
}
// GenerateGo generates go code for the schema
func (s *Schema) GenerateGo() ([]byte, error) {
// Generate file headers
out, err := s.GenerateGoHdrs()
if err != nil {
return nil, err
}
// Generate structs
str, err := s.GenerateGoStructs()
if err != nil {
log.Errorf("Error generating go structs. Err: %v", err)
return nil, err
}
// Merge the header and struct
out = append(out, str...)
// Merge rest handler
str, err = s.GenerateGoFuncs()
if err != nil {
return nil, err
}
out = append(out, str...)
gobytes, err := format.Source(out)
if err != nil {
os.Stderr.Write(out)
return out, err
}
return gobytes, nil
}
// GenerateGoStructs generates go code from a schema
func (s *Schema) GenerateGoStructs() ([]byte, error) {
var goBytes []byte
// Generate all object definitions
for _, obj := range s.Objects {
objBytes, err := obj.GenerateGoStructs()
if err != nil {
return nil, err
}
goBytes = append(goBytes, objBytes...)
}
for _, name := range []string{"gostructs", "callbacks", "init", "objcount", "register"} {
byts, err := generators.RunTemplate(name, s)
if err != nil {
return nil, err
}
goBytes = append(goBytes, byts...)
}
return goBytes, nil
}
// GenerateGoHdrs generates go file headers
func (s *Schema) GenerateGoHdrs() ([]byte, error) {
return generators.RunTemplate("hdr", s)
}
func (s *Schema) GenerateGoFuncs() ([]byte, error) {
// Output the functions and routes
return generators.RunTemplate("routeFunc", s)
}
func (obj *Object) GenerateGoStructs() ([]byte, error) {
return generators.RunTemplate("objstruct", obj)
}
func (prop *Property) GenerateGoStructs() (string, error) {
// this function has to return a string because it is used in templates.
var found bool
for _, myType := range validPropertyTypes {
if myType == prop.Type {
found = true
}
}
if !found {
if _, ok := validObjects[prop.Type]; !ok {
return "", errors.New("Unknown Property")
}
}
byt, err := generators.RunTemplate("propstruct", prop)
return string(byt), err
}