Skip to content

Commit 4da73fb

Browse files
committed
gengo: support for unions with stringprefix representation.
1 parent 80a4a97 commit 4da73fb

File tree

9 files changed

+467
-72
lines changed

9 files changed

+467
-72
lines changed

node/mixins/delim.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ func SplitExact(s string, sep string, count int) ([]string, error) {
3131
}
3232
return ss, nil
3333
}
34+
35+
// SplitN is an alias of strings.SplitN, which is only present here to
36+
// make it usable in codegen packages without requiring conditional imports
37+
// in the generation process.
38+
func SplitN(s, sep string, n int) []string {
39+
return strings.SplitN(s, sep, n)
40+
}

schema/gen/go/README.md

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -47,70 +47,71 @@ Legend:
4747
- `?` - feature definition needed! (applies to many of the "native extras" rows -- often there's partial features, but also room for more.)
4848
- ` ` - table is not finished, please refer to the code and help fix the table :)
4949

50-
| feature | accessors | builders |
51-
|:-------------------------------|:---------:|:--------:|
52-
| structs | ... | ... |
53-
| ... type level |||
54-
| ... native extras | ? | ? |
55-
| ... map representation |||
56-
| ... ... including optional |||
57-
| ... ... including renames |||
58-
| ... ... including implicits |||
59-
| ... tuple representation |||
60-
| ... ... including optional |||
61-
| ... ... including renames | - | - |
62-
| ... ... including implicits |||
63-
| ... stringjoin representation |||
64-
| ... ... including optional | - | - |
65-
| ... ... including renames | - | - |
66-
| ... ... including implicits | - | - |
67-
| ... stringpairs representation |||
68-
| ... ... including optional | | |
69-
| ... ... including renames | | |
70-
| ... ... including implicits | | |
71-
| ... listpairs representation |||
72-
| ... ... including optional | | |
73-
| ... ... including renames | | |
74-
| ... ... including implicits | | |
75-
76-
| feature | accessors | builders |
77-
|:-------------------------------|:---------:|:--------:|
78-
| lists | ... | ... |
79-
| ... type level |||
80-
| ... native extras | ? | ? |
81-
| ... list representation |||
82-
83-
| feature | accessors | builders |
84-
|:-------------------------------|:---------:|:--------:|
85-
| maps | ... | ... |
86-
| ... type level |||
87-
| ... native extras | ? | ? |
88-
| ... map representation |||
89-
| ... stringpairs representation |||
90-
| ... listpairs representation |||
91-
92-
| feature | accessors | builders |
93-
|:-------------------------------|:---------:|:--------:|
94-
| unions | ... | ... |
95-
| ... type level |||
96-
| ... keyed representation |||
97-
| ... envelope representation |||
98-
| ... kinded representation |||
99-
| ... inline representation |||
100-
| ... byteprefix representation |||
101-
102-
| feature | accessors | builders |
103-
|:-------------------------------|:---------:|:--------:|
104-
| strings |||
105-
| bytes |||
106-
| ints |||
107-
| floats |||
108-
| bools |||
109-
| links |||
110-
111-
| feature | accessors | builders |
112-
|:-------------------------------|:---------:|:--------:|
113-
| enums | ... | ... |
114-
| ... type level |||
115-
| ... string representation |||
116-
| ... int representation |||
50+
| feature | accessors | builders |
51+
|:---------------------------------|:---------:|:--------:|
52+
| structs | ... | ... |
53+
| ... type level |||
54+
| ... native extras | ? | ? |
55+
| ... map representation |||
56+
| ... ... including optional |||
57+
| ... ... including renames |||
58+
| ... ... including implicits |||
59+
| ... tuple representation |||
60+
| ... ... including optional |||
61+
| ... ... including renames | - | - |
62+
| ... ... including implicits |||
63+
| ... stringjoin representation |||
64+
| ... ... including optional | - | - |
65+
| ... ... including renames | - | - |
66+
| ... ... including implicits | - | - |
67+
| ... stringpairs representation |||
68+
| ... ... including optional | | |
69+
| ... ... including renames | | |
70+
| ... ... including implicits | | |
71+
| ... listpairs representation |||
72+
| ... ... including optional | | |
73+
| ... ... including renames | | |
74+
| ... ... including implicits | | |
75+
76+
| feature | accessors | builders |
77+
|:---------------------------------|:---------:|:--------:|
78+
| lists | ... | ... |
79+
| ... type level |||
80+
| ... native extras | ? | ? |
81+
| ... list representation |||
82+
83+
| feature | accessors | builders |
84+
|:---------------------------------|:---------:|:--------:|
85+
| maps | ... | ... |
86+
| ... type level |||
87+
| ... native extras | ? | ? |
88+
| ... map representation |||
89+
| ... stringpairs representation |||
90+
| ... listpairs representation |||
91+
92+
| feature | accessors | builders |
93+
|:---------------------------------|:---------:|:--------:|
94+
| unions | ... | ... |
95+
| ... type level |||
96+
| ... keyed representation |||
97+
| ... envelope representation |||
98+
| ... kinded representation |||
99+
| ... inline representation |||
100+
| ... stringprefix representation |||
101+
| ... byteprefix representation |||
102+
103+
| feature | accessors | builders |
104+
|:---------------------------------|:---------:|:--------:|
105+
| strings |||
106+
| bytes |||
107+
| ints |||
108+
| floats |||
109+
| bools |||
110+
| links |||
111+
112+
| feature | accessors | builders |
113+
|:---------------------------------|:---------:|:--------:|
114+
| enums | ... | ... |
115+
| ... type level |||
116+
| ... string representation |||
117+
| ... int representation |||

schema/gen/go/genUnion.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ func (g unionGenerator) EmitNativeType(w io.Writer) {
4040
//
4141
// The interface *mostly* isn't used... except for in the return type of a speciated function which can be used to do golang-native type switches.
4242
//
43+
// The interface also includes a requirement for an errorless primitive access method (such as `String() string`)
44+
// if our representation strategy is one that has that semantic (e.g., stringprefix repr does).
45+
//
4346
// A note about index: in all cases the index of a member type is used, we increment it by one, to avoid using zero.
4447
// We do this because it's desirable to reserve the zero in the 'tag' field (if we generate one) as a sentinel value
4548
// (see further comments in the EmitNodeAssemblerType function);
4649
// and since we do it in that one case, it's just as well to do it uniformly.
4750
doTemplate(`
4851
{{- if Comments -}}
49-
// {{ .Type | TypeSymbol }} matches the IPLD Schema type "{{ .Type.Name }}". It has {{ .Type.TypeKind }} type-kind, and may be interrogated like {{ .Kind }} kind.
52+
// {{ .Type | TypeSymbol }} matches the IPLD Schema type "{{ .Type.Name }}".
53+
// {{ .Type | TypeSymbol }} has {{ .Type.TypeKind }} typekind, which means its data model behaviors are that of a {{ .Kind }} kind.
5054
{{- end}}
5155
type {{ .Type | TypeSymbol }} = *_{{ .Type | TypeSymbol }}
5256
type _{{ .Type | TypeSymbol }} struct {
@@ -61,6 +65,9 @@ func (g unionGenerator) EmitNativeType(w io.Writer) {
6165
}
6266
type _{{ .Type | TypeSymbol }}__iface interface {
6367
_{{ .Type | TypeSymbol }}__member()
68+
{{- if (eq (.Type.RepresentationStrategy | printf "%T") "schema.UnionRepresentation_Stringprefix") }}
69+
String() string
70+
{{- end}}
6471
}
6572
6673
{{- range $member := .Type.Members }}

0 commit comments

Comments
 (0)