Skip to content

Commit

Permalink
Merge pull request #133 from ipld/gengo-union-repr-stringprefix
Browse files Browse the repository at this point in the history
gengo: support for unions with stringprefix representation.
  • Loading branch information
warpfork authored Jan 7, 2021
2 parents 80a4a97 + bc66b1e commit a3d437a
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 72 deletions.
7 changes: 7 additions & 0 deletions node/mixins/delim.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ func SplitExact(s string, sep string, count int) ([]string, error) {
}
return ss, nil
}

// SplitN is an alias of strings.SplitN, which is only present here to
// make it usable in codegen packages without requiring conditional imports
// in the generation process.
func SplitN(s, sep string, n int) []string {
return strings.SplitN(s, sep, n)
}
135 changes: 68 additions & 67 deletions schema/gen/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,70 +47,71 @@ Legend:
- `?` - feature definition needed! (applies to many of the "native extras" rows -- often there's partial features, but also room for more.)
- ` ` - table is not finished, please refer to the code and help fix the table :)

| feature | accessors | builders |
|:-------------------------------|:---------:|:--------:|
| structs | ... | ... |
| ... type level |||
| ... native extras | ? | ? |
| ... map representation |||
| ... ... including optional |||
| ... ... including renames |||
| ... ... including implicits |||
| ... tuple representation |||
| ... ... including optional |||
| ... ... including renames | - | - |
| ... ... including implicits |||
| ... stringjoin representation |||
| ... ... including optional | - | - |
| ... ... including renames | - | - |
| ... ... including implicits | - | - |
| ... stringpairs representation |||
| ... ... including optional | | |
| ... ... including renames | | |
| ... ... including implicits | | |
| ... listpairs representation |||
| ... ... including optional | | |
| ... ... including renames | | |
| ... ... including implicits | | |

| feature | accessors | builders |
|:-------------------------------|:---------:|:--------:|
| lists | ... | ... |
| ... type level |||
| ... native extras | ? | ? |
| ... list representation |||

| feature | accessors | builders |
|:-------------------------------|:---------:|:--------:|
| maps | ... | ... |
| ... type level |||
| ... native extras | ? | ? |
| ... map representation |||
| ... stringpairs representation |||
| ... listpairs representation |||

| feature | accessors | builders |
|:-------------------------------|:---------:|:--------:|
| unions | ... | ... |
| ... type level |||
| ... keyed representation |||
| ... envelope representation |||
| ... kinded representation |||
| ... inline representation |||
| ... byteprefix representation |||

| feature | accessors | builders |
|:-------------------------------|:---------:|:--------:|
| strings |||
| bytes |||
| ints |||
| floats |||
| bools |||
| links |||

| feature | accessors | builders |
|:-------------------------------|:---------:|:--------:|
| enums | ... | ... |
| ... type level |||
| ... string representation |||
| ... int representation |||
| feature | accessors | builders |
|:---------------------------------|:---------:|:--------:|
| structs | ... | ... |
| ... type level |||
| ... native extras | ? | ? |
| ... map representation |||
| ... ... including optional |||
| ... ... including renames |||
| ... ... including implicits |||
| ... tuple representation |||
| ... ... including optional |||
| ... ... including renames | - | - |
| ... ... including implicits |||
| ... stringjoin representation |||
| ... ... including optional | - | - |
| ... ... including renames | - | - |
| ... ... including implicits | - | - |
| ... stringpairs representation |||
| ... ... including optional | | |
| ... ... including renames | | |
| ... ... including implicits | | |
| ... listpairs representation |||
| ... ... including optional | | |
| ... ... including renames | | |
| ... ... including implicits | | |

| feature | accessors | builders |
|:---------------------------------|:---------:|:--------:|
| lists | ... | ... |
| ... type level |||
| ... native extras | ? | ? |
| ... list representation |||

| feature | accessors | builders |
|:---------------------------------|:---------:|:--------:|
| maps | ... | ... |
| ... type level |||
| ... native extras | ? | ? |
| ... map representation |||
| ... stringpairs representation |||
| ... listpairs representation |||

| feature | accessors | builders |
|:---------------------------------|:---------:|:--------:|
| unions | ... | ... |
| ... type level |||
| ... keyed representation |||
| ... envelope representation |||
| ... kinded representation |||
| ... inline representation |||
| ... stringprefix representation |||
| ... byteprefix representation |||

| feature | accessors | builders |
|:---------------------------------|:---------:|:--------:|
| strings |||
| bytes |||
| ints |||
| floats |||
| bools |||
| links |||

| feature | accessors | builders |
|:---------------------------------|:---------:|:--------:|
| enums | ... | ... |
| ... type level |||
| ... string representation |||
| ... int representation |||
9 changes: 8 additions & 1 deletion schema/gen/go/genUnion.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ func (g unionGenerator) EmitNativeType(w io.Writer) {
//
// 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.
//
// The interface also includes a requirement for an errorless primitive access method (such as `String() string`)
// if our representation strategy is one that has that semantic (e.g., stringprefix repr does).
//
// A note about index: in all cases the index of a member type is used, we increment it by one, to avoid using zero.
// We do this because it's desirable to reserve the zero in the 'tag' field (if we generate one) as a sentinel value
// (see further comments in the EmitNodeAssemblerType function);
// and since we do it in that one case, it's just as well to do it uniformly.
doTemplate(`
{{- if Comments -}}
// {{ .Type | TypeSymbol }} matches the IPLD Schema type "{{ .Type.Name }}". It has {{ .Type.TypeKind }} type-kind, and may be interrogated like {{ .Kind }} kind.
// {{ .Type | TypeSymbol }} matches the IPLD Schema type "{{ .Type.Name }}".
// {{ .Type | TypeSymbol }} has {{ .Type.TypeKind }} typekind, which means its data model behaviors are that of a {{ .Kind }} kind.
{{- end}}
type {{ .Type | TypeSymbol }} = *_{{ .Type | TypeSymbol }}
type _{{ .Type | TypeSymbol }} struct {
Expand All @@ -61,6 +65,9 @@ func (g unionGenerator) EmitNativeType(w io.Writer) {
}
type _{{ .Type | TypeSymbol }}__iface interface {
_{{ .Type | TypeSymbol }}__member()
{{- if (eq (.Type.RepresentationStrategy | printf "%T") "schema.UnionRepresentation_Stringprefix") }}
String() string
{{- end}}
}
{{- range $member := .Type.Members }}
Expand Down
Loading

0 comments on commit a3d437a

Please sign in to comment.