Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Restructure constant generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Oct 21, 2020
1 parent 5d59296 commit 837dad2
Show file tree
Hide file tree
Showing 8 changed files with 1,473 additions and 1,460 deletions.
940 changes: 940 additions & 0 deletions codec_string.go

Large diffs are not rendered by default.

926 changes: 0 additions & 926 deletions codecs.go

This file was deleted.

918 changes: 459 additions & 459 deletions const.go

Large diffs are not rendered by default.

21 changes: 0 additions & 21 deletions gen/codec.go

This file was deleted.

7 changes: 3 additions & 4 deletions templates/const.go.tpl → gen/const.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type Codec uint64

// These are multicodec-packed content types.
const (
{{ range .Codecs }}// {{ if .IsDeprecated }}Deprecated: {{ end }}{{ .Tag }}{{ if .Description }}: {{ .Description }}{{ end }}
{{ .VarName }} Codec = {{ .Code }}
{{ end }}
)
{{ range . }}// {{ if .IsDeprecated }}Deprecated: {{ end }}{{ .Tag }}{{ if .Description }}: {{ .Description }}{{ end }}
{{ .VarName }} Codec = {{ .Code }} // {{ .Name }}
{{ end }})
106 changes: 70 additions & 36 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,59 @@ import (
"text/template"
)

// TemplateData is the data structure that is passed into the go template
type TemplateData struct {
Codecs []Codec
// Codec represents a row in the canonical multicodec table
type Codec struct {
Name string
Tag string
Code string
Description string
}

// IsDeprecated returns true if the description contains the word deprecated
func (c Codec) IsDeprecated() bool {
return strings.Contains(strings.ToLower(c.Description), "deprecated")
}

// VarName returns the variable name to be used in Go for this codec
// Implementation adapted from https://github.com/iancoleman/strcase
func (c Codec) VarName() string {
if c.Name == "" {
return c.Name
}

// convert the string to camelcase
n := strings.Builder{}
n.Grow(len(c.Name))
capNext := true
for i, v := range []byte(c.Name) {
vIsCap := v >= 'A' && v <= 'Z'
vIsLow := v >= 'a' && v <= 'z'
vIsNum := v >= '0' && v <= '9'
if capNext {
if vIsLow {
v += 'A'
v -= 'a'
}
} else if i == 0 {
if vIsCap {
v += 'a'
v -= 'A'
}
}
if vIsCap || vIsLow {
n.WriteByte(v)
capNext = false
} else if vIsNum {
n.WriteByte(v)
capNext = true
} else {
capNext = v == '_' || v == ' ' || v == '-' || v == '.'
if capNext {
n.WriteByte('_')
}
}
}
return n.String()
}

func main() {
Expand All @@ -28,48 +78,32 @@ func main() {
}

codecs := make([]Codec, len(records)-1)
for i, record := range records {

// Skip header line
if i == 0 {
continue
}

c := Codec{
for i, record := range records[1:] { // Skip header line
codecs[i] = Codec{
Name: strings.TrimSpace(record[0]),
Tag: strings.TrimSpace(record[1]),
Code: strings.TrimSpace(record[2]),
Description: strings.TrimSpace(record[3]),
}
codecs[i-1] = c
}

tData := TemplateData{
Codecs: codecs,
tplFileName := "const.go.tpl"
t, err := template.
New(tplFileName).
Funcs(template.FuncMap{"ToTitle": strings.Title}).
ParseFiles(path.Join("gen", tplFileName))
if err != nil {
log.Fatal(err)
}

templates := []string{"codecs", "const"}
for _, templateName := range templates {

tplFileName := templateName + ".go.tpl"
t, err := template.
New(tplFileName).
Funcs(template.FuncMap{"ToTitle": strings.Title}).
ParseFiles(path.Join("templates", tplFileName))
if err != nil {
log.Fatal(err)
}

out, err := os.Create(templateName + ".go")
if err != nil {
log.Fatal(err)
}
out, err := os.Create("const.go")
if err != nil {
log.Fatal(err)
}
defer out.Close()

err = t.Execute(out, tData)
if err != nil {
out.Close()
log.Fatal(err)
}
out.Close()
err = t.Execute(out, codecs)
if err != nil {
log.Fatal(err)
}
}
1 change: 1 addition & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ package multicodec

//go:generate go run ./gen/
//go:generate go fmt ./...
//go:generate stringer -type=Codec -linecomment
14 changes: 0 additions & 14 deletions templates/codecs.go.tpl

This file was deleted.

0 comments on commit 837dad2

Please sign in to comment.