-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtonel.go
150 lines (128 loc) · 2.78 KB
/
tonel.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
)
type tonelWriter struct {
buff *bytes.Buffer
model *YamlModel
}
func writeTonelFiles(args *args) {
model, err := ReadYamlModel(args)
check(err, "could not read YAML model")
outDir := filepath.Join(args.home, "build", "tonel")
mkdir(outDir)
model.EachClass(func(class *YamlClass) {
var buffer bytes.Buffer
writer := tonelWriter{
buff: &buffer,
model: model,
}
writer.writeClass(class)
outFile := filepath.Join(outDir, "Lca"+class.Name+".class.st")
os.WriteFile(outFile, buffer.Bytes(), os.ModePerm)
})
}
func (w *tonelWriter) writeClass(class *YamlClass) {
className := "Lca" + class.Name
// class declaration
w.writeln("Class {")
w.writeln("\t#name : #", className, ",")
var super string
if class.SuperClass != "" {
super = "Lca" + class.SuperClass
} else {
super = "LcaEntity"
}
w.writeln("\t#superclass : #", super, ",")
w.writeln("\t#instVars : [")
for i, prop := range class.Props {
propName := w.propNameOf(prop)
if propName == "" {
continue
}
if i < (len(class.Props) - 1) {
w.writeln("\t\t'", propName, "',")
} else {
w.writeln("\t\t'", propName, "'")
}
}
w.writeln("\t],")
w.writeln("\t#category : #'openLCA-Model'")
w.writeln("}")
// accessors
for _, prop := range class.Props {
propName := w.propNameOf(prop)
if propName == "" {
continue
}
w.writeln()
// getter
w.writeln("{ #category : #accessing }")
w.writeln(className, " >> ", propName, " [")
w.writeln()
w.writeln("\t^ ", propName)
w.writeln("]")
w.writeln()
// setter
typeHint := w.typeHintOf(prop)
w.writeln("{ #category : #accessing }")
w.writeln(className, " >> ", propName, ": ", typeHint, " [")
w.writeln()
w.writeln("\t", propName, " := ", typeHint)
w.writeln("]")
}
}
func (w *tonelWriter) propNameOf(prop *YamlProp) string {
switch prop.Name {
case "@type":
return ""
case "@id":
return "id"
default:
return prop.Name
}
}
func (w *tonelWriter) typeHintOf(prop *YamlProp) string {
propType := prop.PropType()
if propType.IsList() {
return "aCollection"
}
if propType.IsRef() {
return "aRef"
}
if !propType.IsPrimitive() {
b0 := prop.Type[0]
prefix := "a"
switch b0 {
case 'A', 'E', 'I', 'O', 'U':
prefix = "an"
}
return prefix + prop.Type
}
switch prop.Type {
case "string":
return "aString"
case "int", "integer":
return "anInteger"
case "double":
return "aNumber"
case "bool", "boolean":
return "aBoolean"
case "date":
return "aDateString"
case "dateTime":
return "aDateTimeString"
default:
fmt.Println("warning: could provide a better type hint for:" + prop.Type)
return "anObject"
}
}
func (w *tonelWriter) writeln(xs ...string) {
for _, x := range xs {
w.buff.WriteString(x)
}
w.buff.WriteRune('\n')
}