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

Remove tags.go generation #5

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }})
131 changes: 69 additions & 62 deletions gen/gen.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
package main

//go:generate go run ./gen/gen.go
//go:generate gofmt -w codecs.go

import (
"encoding/csv"
"log"
"os"
"path"
"sort"
"strings"
"text/template"
)

var templates = []string{
"codecs",
"const",
"tag",
// Codec represents a row in the canonical multicodec table
type Codec struct {
Name string
Tag string
Code string
Description string
}

// TemplateData is the data structure that is passed into the go template
type TemplateData struct {
Codecs []Codec
CodecsByTag map[string][]Codec
Tags []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 @@ -34,69 +72,38 @@ func main() {
}
defer table.Close()

reader := csv.NewReader(table)
records, err := reader.ReadAll()
records, err := csv.NewReader(table).ReadAll()
if err != nil {
log.Fatal(err)
}

tags := []string{}
codecsByTag := map[string][]Codec{}
codecs := []Codec{}
for i, record := range records {

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

c := Codec{
codecs := make([]Codec, len(records)-1)
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 = append(codecs, c)

if _, exists := codecsByTag[c.Tag]; !exists {
tags = append(tags, c.Tag)
codecsByTag[c.Tag] = []Codec{}
}
codecsByTag[c.Tag] = append(codecsByTag[c.Tag], c)
}

sort.Strings(tags)

tData := TemplateData{
Codecs: codecs,
CodecsByTag: codecsByTag,
Tags: tags,
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)
}

funcMap := template.FuncMap{
"ToTitle": strings.Title,
out, err := os.Create("const.go")
if err != nil {
log.Fatal(err)
}
defer out.Close()

for _, templateName := range templates {

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

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

err = t.Execute(out, tData)
if err != nil {
log.Fatal(err)
}
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
Loading