Skip to content

Commit

Permalink
Merge pull request heetch#53 from heetch/031-fix-go-names
Browse files Browse the repository at this point in the history
cmd/avro-generate-go: respect go.name when generating type names
  • Loading branch information
rogpeppe authored Feb 13, 2020
2 parents 30a95e1 + 1a4fc66 commit 357e7ea
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package goTypeCustomName

// Check that the types exist and look correct
var (
_ customName
_ customEnum = customEnumA
_ = customFixed{}[1]
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "M",
"type": "record",
"fields": [
{
"name": "E",
"type": {
"name": "e",
"type": "enum",
"symbols": [
"a",
"b"
],
"go.name": "customEnum"
}
},
{
"name": "F",
"type": {
"name": "f",
"type": "fixed",
"size": 2,
"go.name": "customFixed"
}
}
],
"go.name": "customName"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 32 additions & 20 deletions cmd/avro-generate-go/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/template"

"github.com/rogpeppe/gogen-avro/v7/parser"
"github.com/rogpeppe/gogen-avro/v7/schema"
)

func newTemplate(s string) *template.Template {
Expand All @@ -21,6 +22,8 @@ func newTemplate(s string) *template.Template {
var templateFuncs = template.FuncMap{
"typeof": typeof,
"isExportedGoIdentifier": isExportedGoIdentifier,
"defName": defName,
"symbolName": symbolName,
"indent": indent,
"doc": doc,
"import": func(gc *generateContext, pkg string) string {
Expand Down Expand Up @@ -58,7 +61,7 @@ var bodyTemplate = newTemplate(`
«- else if $.Ctx.IsExternal $def»«/* Omit the external definition */»
«- else if eq (typeof .) "RecordDefinition"»
«- doc "// " .»
type «.Name» struct {
type «defName .» struct {
«- range $i, $_ := .Fields»
«- doc "\t// " .»
«- $type := $.Ctx.GoTypeOf .Type»
Expand All @@ -72,63 +75,71 @@ var bodyTemplate = newTemplate(`
}
// AvroRecord implements the avro.AvroRecord interface.
func («.Name») AvroRecord() avrotypegen.RecordInfo {
func («defName .») AvroRecord() avrotypegen.RecordInfo {
return «$.Ctx.RecordInfoLiteral .»
}
«else if eq (typeof .) "EnumDefinition"»
«- import $.Ctx "strconv"»
«- import $.Ctx "fmt"»
«- doc "// " . -»
type «.Name» int
type «defName .» int
const (
«- range $i, $sym := .Symbols»
«$def.SymbolName $sym»«if eq $i 0» «$def.Name» = iota«end»
«symbolName $def $sym»«if eq $i 0» «defName $def» = iota«end»
«- end»
)
var _«.Name»_strings = []string{
var _«defName .»_strings = []string{
«range $i, $sym := .Symbols»
«- printf "%q" $sym»,
«end»}
// String returns the textual representation of «.Name».
func (e «.Name») String() string {
if e < 0 || int(e) >= len(_«.Name»_strings) {
return "«.Name»(" + strconv.FormatInt(int64(e), 10) + ")"
// String returns the textual representation of «defName .».
func (e «defName .») String() string {
if e < 0 || int(e) >= len(_«defName .»_strings) {
return "«defName .»(" + strconv.FormatInt(int64(e), 10) + ")"
}
return _«.Name»_strings[e]
return _«defName .»_strings[e]
}
// MarshalText implements encoding.TextMarshaler
// by returning the textual representation of «.Name».
func (e «.Name») MarshalText() ([]byte, error) {
if e < 0 || int(e) >= len(_«.Name»_strings) {
return nil, fmt.Errorf("«.Name» value %d is out of bounds", e)
// by returning the textual representation of «defName .».
func (e «defName .») MarshalText() ([]byte, error) {
if e < 0 || int(e) >= len(_«defName .»_strings) {
return nil, fmt.Errorf("«defName .» value %d is out of bounds", e)
}
return []byte(_«.Name»_strings[e]), nil
return []byte(_«defName .»_strings[e]), nil
}
// UnmarshalText implements encoding.TextUnmarshaler
// by expecting the textual representation of «.Name».
func (e *«.Name») UnmarshalText(data []byte) error {
func (e *«defName .») UnmarshalText(data []byte) error {
// Note for future: this could be more efficient.
for i, s := range _«.Name»_strings {
for i, s := range _«defName .»_strings {
if string(data) == s {
*e = «.Name»(i)
*e = «defName .»(i)
return nil
}
}
return fmt.Errorf("unknown value %q for «.Name»", data)
return fmt.Errorf("unknown value %q for «defName .»", data)
}
«else if eq (typeof .) "FixedDefinition"»
«- doc "// " . -»
type «.Name» [«.SizeBytes»]byte
type «defName .» [«.SizeBytes»]byte
«else»
// unknown definition type «printf "%T; name %q" . (typeof .)» .
«end»
«end»
`[1:])

func defName(def schema.Definition) string {
return goTypeForDefinition(def).Name
}

func symbolName(e *schema.EnumDefinition, symbol string) string {
return defName(e) + strings.Title(symbol)
}

func quote(s string) string {
if !strings.Contains(s, "`") {
return "`" + s + "`"
Expand Down Expand Up @@ -160,6 +171,7 @@ var goIdentifierPat = regexp.MustCompile(`^[A-Z][a-zA-Z_0-9]*$`)
func isExportedGoIdentifier(s string) bool {
return goIdentifierPat.MatchString(s)
}

func typeof(x interface{}) string {
if x == nil {
return "nil"
Expand Down
17 changes: 8 additions & 9 deletions cmd/avro-generate-go/testdata/cloudevent.cue
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package roundtrip


tests: cloudEvent: {
DomainName :: "someDomain"
EventName :: "someEvent"
Version :: "v9.9.99"
Version :: "v9.9.99"
inSchema: {
type: "record"
name: "com.heetch.\(DomainName).\(EventName)"
type: "record"
name: "com.heetch.\(DomainName).\(EventName)"
heetchmeta: {
commentary: "This Schema describes version \(Version) of the event \(EventName) from the domain \(DomainName)."
topickey: "\(DomainName).\(EventName).\(Version)"
Expand Down Expand Up @@ -48,8 +47,8 @@ tests: cloudEvent: {
}
goType: "Message"
outSchema: {
type: "record"
name: "com.heetch.Message"
type: "record"
name: "com.heetch.Message"
fields: [{
name: "Metadata"
type: {
Expand Down Expand Up @@ -83,10 +82,10 @@ tests: cloudEvent: {
}
inData: {
Metadata: CloudEvent: {
id: "id1"
source: "source1"
id: "id1"
source: "source1"
specversion: "someversion"
time: 1580392724000000
time: 1580392724000000
}
other: "some other data"
}
Expand Down
Loading

0 comments on commit 357e7ea

Please sign in to comment.