/
tmpl_union.go
146 lines (134 loc) · 4.19 KB
/
tmpl_union.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package swift
import (
"bytes"
"fmt"
"log"
"v.io/v23/vdl"
"v.io/x/ref/lib/vdl/compile"
)
const unionTmpl = `{{ .Doc }}///
/// Auto-generated from {{.VdlTypeName}}
{{ .AccessModifier }} enum {{.Name}} : VdlUnion {{ if .IsHashable }}, Hashable {{ end }} {
/// Vdl type for {@link {{.Name}}}.
{{ .AccessModifier }} static let vdlTypeName = "{{.VdlTypeName}}"
{{ range $index, $field := .Fields }}
{{ $field.Doc }}
/// GeneratedFromVdl(name = "{{$field.VdlName}}", index = {{$index}})
case {{$field.Name}}(elem:{{$field.Type}})
{{ end }}
{{ if .IsHashable }}
{{ .AccessModifier }} var hashValue: Int {
switch self {
{{ range $field := .Fields }}
case let .{{ $field.Name }}(elem): return {{$field.HashcodeComputation}}
{{ end }}
}
}
{{ end }}
{{ .AccessModifier }} var vdlName: String {
switch self {
{{ range $field := .Fields }}
case .{{ $field.Name }}: return "{{$field.VdlName}}"
{{ end }}
}
}
{{ .AccessModifier }} var description: String {
return vdlName
}
{{ .AccessModifier }} var debugDescription: String {
switch self {
{{ range $field := .Fields }}
{{ if .IsAny }}
case let .{{ $field.Name }}(elem): return "{{$field.VdlName}}(\(elem))"
{{ else }}
case let .{{ $field.Name }}(elem): return "{{$field.VdlName}}(\(elem.debugDescription))"
{{ end }}{{/* IsAny */}}
{{ end }}{{/* range */}}
}
}
}
{{ if .IsHashable }}
{{ .AccessModifier }} func ==(lhs: {{.Name}}, rhs: {{.Name}}) -> Bool {
switch (lhs, rhs) {
{{ $EnumName := .Name }}
{{ range $field := .Fields }}
case ({{$EnumName}}.{{ $field.Name }}(let lElem), {{$EnumName}}.{{ $field.Name }}(let rElem)):
{{ if $field.IsOptional }}
switch (lElem, rElem) {
case (nil, nil): return true
case (nil, _): return false
case (_, nil): return false
case let (lfield?, rfield?): return lfield == rfield
default: return false
}
{{ else }}
return lElem == rElem
{{ end }} {{/* optional comparison */}}
{{ end }}
default: return false
}
}{{ end }}`
type unionDefinitionField struct {
AccessModifier string
Class string
Doc string
HashcodeComputation string
IsAny bool
IsOptional bool
Name string
Type string
VdlName string
}
// genSwiftUnionFile generates the Swift class file for the provided user-defined union type.
func genSwiftUnionTdef(tdef *compile.TypeDef, ctx *swiftContext) string {
fields := make([]unionDefinitionField, tdef.Type.NumField())
isHashable := isTypeHashable(tdef.Type, make(map[*vdl.Type]bool))
for i := 0; i < tdef.Type.NumField(); i++ {
fld := tdef.Type.Field(i)
hashComp := ""
if isHashable {
var err error
hashComp, err = ctx.swiftHashCode("elem", fld.Type)
if err != nil {
panic(fmt.Sprintf("Unable to compute hashcode: %v", err))
}
}
fields[i] = unionDefinitionField{
AccessModifier: swiftAccessModifierForName(fld.Name),
Class: ctx.swiftType(fld.Type),
Doc: swiftDoc(tdef.FieldDoc[i], tdef.FieldDocSuffix[i]),
HashcodeComputation: hashComp,
IsAny: fld.Type.Kind() == vdl.Any,
IsOptional: fld.Type.Kind() == vdl.Optional,
Name: swiftCaseName(tdef, fld),
Type: ctx.swiftType(fld.Type),
VdlName: fld.Name,
}
}
data := struct {
AccessModifier string
Doc string
Fields []unionDefinitionField
IsHashable bool
Name string
Source string
VdlTypeName string
}{
AccessModifier: swiftAccessModifier(tdef),
Doc: swiftDoc(tdef.Doc, tdef.DocSuffix),
Fields: fields,
IsHashable: isHashable,
Name: ctx.swiftTypeName(tdef),
Source: tdef.File.BaseName,
VdlTypeName: tdef.Type.Name(),
}
var buf bytes.Buffer
err := parseTmpl("swift union", unionTmpl).Execute(&buf, data)
if err != nil {
log.Fatalf("vdl: couldn't execute union template: %v", err)
}
return formatSwiftCode(buf.String())
}