This repository was archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontext.go
More file actions
313 lines (281 loc) · 9.13 KB
/
Copy pathcontext.go
File metadata and controls
313 lines (281 loc) · 9.13 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Copyright (C) 2014 Pivotal Software, Inc.
All rights reserved. This program and the accompanying materials
are made available under the terms of the 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 levo
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Context struct {
ProjectName string
PackageName string
TemplaterVersion string
Schema Schema
Templates []TemplateInfo
Mappings []TemplatesForModels
Language string
TemplateFeatures map[string]bool
GoAdapter GoTemplateAdapter
}
type Schema struct {
Project string
Models []Model
}
type Model struct {
Name string
Parent string
ParentRef *Model
Properties []ModelProperty
}
type ModelProperty struct {
RemoteIdentifier string
LocalIdentifier string
PropertyType string
IsSetType bool
}
type TemplateInfo struct {
Language string
Version string
Directory string
FileName string
Body []byte
Adapter OutputAdapter
}
type TemplatesForModels struct {
Models []*Model
Templates []*TemplateInfo
}
type GeneratedFile struct {
FileName string
Directory string
Body []byte
}
func (context *Context) AddModel(model Model) (*Model, error) {
fmt.Printf("")
_, err := context.ModelForName(model.Name)
if err == nil {
return &Model{}, errors.New("Attempted to add duplicate model with name " + model.Name)
}
for index, _ := range model.Properties {
if model.Properties[index].LocalIdentifier == "" {
model.Properties[index].LocalIdentifier = model.Properties[index].RemoteIdentifier
}
}
context.Schema.Models = append(context.Schema.Models, model)
return &(context.Schema.Models[len(context.Schema.Models)-1]), nil
}
func (context *Context) AddModelWithName(name string) (*Model, error) {
if name == "" {
return &Model{}, errors.New("Model name must not be empty string")
}
model := Model{Name: name}
return context.AddModel(model)
}
func (context *Context) AddTemplateDirectory(templateDirPath string) ([]TemplateInfo, error) {
err := filepath.Walk(templateDirPath, context.AddTemplateFile)
if err != nil {
return []TemplateInfo{}, err
}
templateDirPath = strings.TrimSuffix(templateDirPath, "/")
for index, template := range context.Templates {
//Remove the first part of the directory path so that generated
//files are relative to the working directory, not the template
//directory
if templateDirPath != context.Templates[index].Directory {
context.Templates[index].Directory = template.Directory[len(templateDirPath)+1:]
} else {
context.Templates[index].Directory = ""
}
}
return context.Templates, nil
}
func (context *Context) AddTemplateFilePath(filePath string) (TemplateInfo, error) {
fileInfo, err := os.Stat(filePath)
if err != nil {
return TemplateInfo{}, err
}
fileContents, err := ioutil.ReadFile(filePath)
if err != nil {
return TemplateInfo{}, err
}
fileName := fileInfo.Name()
templateVersion := ""
if fileName[len(fileName)-2:] == "lt" {
templateVersion = "1.0.0"
}
templateInfo, err := context.AddTemplate(fileName, fileContents, templateVersion, "", &context.GoAdapter)
if err != nil {
return TemplateInfo{}, err
}
return *templateInfo, nil
}
func (context *Context) AddTemplateFile(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info == nil {
return errors.New("Nil file info when adding template file " + path)
}
if info.IsDir() {
if info.Name() == ".git" || info.Name() == ".hg" {
return filepath.SkipDir
}
} else if info.Name() == ".DS_Store" {
//do nothing
} else {
//it's a template! We should add it!
fileContents, err := ioutil.ReadFile(path)
if err != nil {
return err
}
fileName := info.Name()
directory := path[0 : len(path)-len(fileName)]
templateVersion := ""
if fileName[len(fileName)-2:] == "lt" {
templateVersion = "1.0.0"
}
_, err = context.AddTemplate(fileName, fileContents, templateVersion, directory, &context.GoAdapter)
if err != nil {
return err
}
}
return nil
}
func (context *Context) AddTemplate(fileName string, body []byte, version string, directory string, adapter OutputAdapter) (*TemplateInfo, error) {
if fileName == "" {
return &TemplateInfo{}, errors.New("TemplateInfo must have a filename")
}
if strings.HasSuffix(fileName, ".lt") == false {
encodedBody := make([]byte, base64.StdEncoding.EncodedLen(len(body)))
base64.StdEncoding.Encode(encodedBody, body)
prefix := []byte("<<levobase64>>")
body = append(prefix, encodedBody...)
version = LibraryVersion
}
_, err := context.FindTemplate(fileName, directory)
if err == nil {
return &TemplateInfo{}, errors.New("Attempted to add duplicate template with name " + fileName)
}
templateInfo := TemplateInfo{FileName: fileName, Body: body, Directory: directory, Version: version, Adapter: adapter, Language: context.Language}
context.Templates = append(context.Templates, templateInfo)
return &templateInfo, nil
}
func (context *Context) FindTemplate(fileName string, directory string) (*TemplateInfo, error) {
for _, template := range context.Templates {
if template.FileName == fileName && template.Directory == directory {
return &template, nil
}
}
return &TemplateInfo{}, errors.New("Template not found: " + fileName)
}
func (context *Context) TemplateForFileName(fileName string) ([]*TemplateInfo, error) {
tempTemplates := make([]*TemplateInfo, 0)
for index, template := range context.Templates {
if template.FileName == fileName {
tempTemplates = append(tempTemplates, &context.Templates[index])
}
}
if len(tempTemplates) > 0 {
return tempTemplates, nil
}
return tempTemplates, errors.New("Template not found: " + fileName)
}
func (context *Context) ModelForName(name string) (*Model, error) {
for _, model := range context.Schema.Models {
if model.Name == name {
return &model, nil
}
}
return &Model{}, errors.New("Model not found: " + name)
}
func (context *Context) AddTemplatesForModelsMapping(templateFileNames []string, modelNames []string) error {
templates := make([]*TemplateInfo, 0)
models := make([]*Model, 0)
for _, fileName := range templateFileNames {
templateInfos, err := context.TemplateForFileName(fileName)
if err != nil {
return err
}
for _, templateInfo := range templateInfos {
templates = appendIfUnique(templates, templateInfo)
}
}
for _, modelName := range modelNames {
model, err := context.ModelForName(modelName)
if err == nil {
models = append(models, model)
} else {
return err
}
}
if len(templates) <= 0 {
return errors.New("Mapping must have at least one template")
}
context.Mappings = append(context.Mappings, TemplatesForModels{Models: models, Templates: templates})
return nil
}
func (context *Context) AddTemplateFeature(feature string) {
context.TemplateFeatures[strings.ToLower(feature)] = true
}
func (context *Context) RemoveTemplateFeature(feature string) {
context.TemplateFeatures[strings.ToLower(feature)] = false
}
func (model *Model) AddProperty(remoteIdentifier string, localIdentifier string, propertyType string) (*ModelProperty, error) {
if remoteIdentifier == "" && localIdentifier == "" {
return &ModelProperty{}, errors.New("Properties must have an identifier")
}
for _, property := range model.Properties {
if property.LocalIdentifier == localIdentifier {
return &ModelProperty{}, errors.New("Attempted to add duplicate properties with name " + localIdentifier)
}
}
if propertyType == "" {
return &ModelProperty{}, errors.New("Properties must have a type")
}
isSetType := false
if strings.HasPrefix(propertyType, "[]") {
propertyType = propertyType[2:]
isSetType = true
} else if strings.HasSuffix(propertyType, "[]") {
propertyType = propertyType[:len(propertyType)-2]
isSetType = true
}
property := ModelProperty{RemoteIdentifier: remoteIdentifier, LocalIdentifier: localIdentifier, PropertyType: propertyType, IsSetType: isSetType}
model.Properties = append(model.Properties, property)
return &(model.Properties[len(model.Properties)-1]), nil
}
func (self *Schema) validate() error {
for _, model := range self.Models {
if model.Name == "" {
return errors.New("At least one model missing Remote Identifier")
}
for _, property := range model.Properties {
if property.RemoteIdentifier == "" {
return errors.New("Model " + model.Name + " has at least one property missing it's Remote Identifier. Type: " + property.PropertyType)
}
}
}
//TODO fill this out more?
return nil
}
func appendIfUnique(slice []*TemplateInfo, item *TemplateInfo) []*TemplateInfo {
for _, existingTemplate := range slice {
if existingTemplate.FileName == item.FileName && existingTemplate.Directory == item.Directory {
return slice
}
}
return append(slice, item)
}