-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_table.go
112 lines (99 loc) · 3.83 KB
/
create_table.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
package catalog
import (
"fmt"
"sort"
"strings"
"github.com/anz-bank/sysl/pkg/sysl"
)
// DataModelReturnTable prints out a markdown table for a given statement and endpoint
func (p *Generator) DataModelReturnTable(appName string, stmt *sysl.Statement, endpoint *sysl.Endpoint) string {
appName, typeName, _, _ := p.ExtractReturnInfo(appName, stmt, endpoint)
if appName == "" && typeName == "" {
return ""
}
return p.DataModelTable(appName, typeName, "")
}
// DataModelAliasTable prints out a markdown table for a given application and parameter
func (p *Generator) DataModelAliasTable(app *sysl.Application, param Param) string {
defer func() {
if err := recover(); err != nil {
p.Log.Errorf("error creating param data model: %s", err)
}
}()
info, typeName, aliasTypeName, _ := p.ExtractTypeInfo(app, param)
return p.DataModelTable(info, typeName, aliasTypeName)
}
// DataModelTable prints out a markdown table which describes a type
func (p *Generator) DataModelTable(appName, typeName, aliasName string) string {
var markdownTable string
if typeName == "" {
return ""
}
if appName == "primitive" {
return printPrimitiveTable(typeName, aliasName)
}
simpleType, ok := p.Mapper.SimpleTypes[appName+"."+typeName]
if !ok {
p.Log.Errorf("Unable to find type: %s.%s with alias %s", appName, typeName, aliasName)
return ""
}
markdownTable += fmt.Sprintf("%s %s \n", simpleType.Type, typeName)
markdownTable += "| Field name | Type | Description |\n"
markdownTable += "|----|----|----|\n"
switch simpleType.Type {
case "enum":
var enumKeys []int
for k := range simpleType.Enum {
enumKeys = append(enumKeys, int(k))
}
sort.Ints(enumKeys)
for _, k := range enumKeys {
markdownTable += fmt.Sprintf("| %d | %s | %s |\n", k, simpleType.Enum[int64(k)], "")
}
case "tuple", "map", "relation":
for _, fieldName := range SortedKeys(simpleType.Properties) {
field := simpleType.Properties[fieldName]
switch simpleType.Properties[fieldName].Type {
case "ref":
reference, ok := p.Mapper.SimpleTypes[field.Reference]
if !ok {
p.Log.Errorf("Unable to find type: %s with alias %s", field.Reference, fieldName)
markdownTable += fmt.Sprintf("| %s | %s | %s |\n", fieldName, field.Reference, field.Description)
} else {
markdownTable += fmt.Sprintf("| %s | %s (%s) | %s |\n", fieldName, convertReferenceToLink(field.Reference), reference.Type, field.Description)
}
case "list":
markdownTable += fmt.Sprintf("| %s | sequence of %s | %s |\n", fieldName, convertReferenceToLink(field.Items[0].Reference), field.Description)
default:
markdownTable += fmt.Sprintf("| %s | %s | %s |\n", fieldName, field.Type, field.Description)
}
}
case "list":
for _, field := range simpleType.Items {
if field.Type == "ref" {
markdownTable += fmt.Sprintf("| %s | sequence of %s | %s |\n", typeName, convertReferenceToLink(field.Reference), field.Description)
} else {
markdownTable += fmt.Sprintf("| %s | sequence of %s | %s |\n", typeName, field.Type, field.Description)
}
}
case "ref":
markdownTable += fmt.Sprintf("| %s | %s | %s |\n", typeName, convertReferenceToLink(simpleType.Reference), simpleType.Description)
}
return markdownTable
}
func printPrimitiveTable(aliasName, typeName string) (markdownTable string) {
lowerCaseTypeName := strings.ToLower(typeName)
markdownTable += aliasName + "<:" + lowerCaseTypeName + "\n"
markdownTable += "| Field name | Type | Description |\n"
markdownTable += "|----|----|----|\n"
markdownTable += fmt.Sprintf("| %s | %s | %s |\n", aliasName, lowerCaseTypeName, "")
return
}
func convertReferenceToLink(reference string) string {
safeReference := SanitiseOutputName(reference)
typeName := strings.Split(reference, ".")
if len(typeName) < 2 {
return "" // Not a valid reference
}
return fmt.Sprintf(`<a href="#%s">%s</a>`, safeReference, typeName[1])
}