Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate gofumpt and go-licenser compliant code #1

Merged
merged 3 commits into from
Sep 16, 2023
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
16 changes: 15 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ jobs:
go-version: '1.21'

- name: Test
run: go test ./...
run: go test ./...

test-generator:
name: test-generator
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version: '1.21'

- name: Test
working-directory: ./internal/generator
run: go run .
6 changes: 6 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jobs:
with:
version: latest

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
working-directory: ./internal/generator

go-mod-tidy:
name: go-mod-tidy
runs-on: ubuntu-latest
Expand Down
7 changes: 3 additions & 4 deletions internal/generator/ecs_flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,18 @@ func (f *field) isArray() bool {
case string:
if v == "array" {
return true
} else {
panic(fmt.Errorf("unhandled value for 'normalize' field of %q: %q", f.Name, v))
}

panic(fmt.Errorf("unhandled value for 'normalize' field of %q: %q", f.Name, v))
default:
panic(fmt.Errorf("unhandled value type for 'normalize' field value type %T in field %q", item, f.Name))
}
}
case string:
if v == "array" {
return true
} else {
panic(fmt.Errorf("unhandled value for 'normalize' field of %q: %q", f.Name, v))
}
panic(fmt.Errorf("unhandled value for 'normalize' field of %q: %q", f.Name, v))
case nil:
default:
panic(fmt.Errorf("unhandled value type for 'normalize' field value type %T in field %q", f.Normalize, f.Name))
Expand Down
46 changes: 40 additions & 6 deletions internal/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

"github.com/coreos/go-semver/semver"
"github.com/dave/jennifer/jen"
"github.com/elastic/go-licenser/licensing"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/mitchellh/hashstructure"
Expand All @@ -45,8 +46,28 @@ const (
appName = "ecs-generator"
remoteRepository = "https://github.com/elastic/ecs"
flatFilePath = "generated/ecs/ecs_flat.yml"

license = "ASL2"
licensor = "Elasticsearch B.V."
)

var licenseHeader string

func init() {
var sb strings.Builder
for _, line := range licensing.Headers[license] {
if strings.Contains(line, "%s") {
fmt.Fprintf(&sb, line, licensor)
sb.WriteByte('\n')
continue
}
sb.WriteString(line)
sb.WriteByte('\n')
}

licenseHeader = sb.String()
}

// Parameters
var (
fetch bool
Expand Down Expand Up @@ -339,6 +360,8 @@ var structListOptions = jen.Options{
func generateVersionFieldArray(version string, fields []ecsField, w io.Writer) error {
f := jen.NewFile("version")

f.HeaderComment(licenseHeader)

f.PackageComment("Code generated by generator, DO NOT EDIT.")

f.Var().Id("v"+strings.ReplaceAll(version, ".", "_")).Op("=").Map(jen.String()).Id("*Field").
Expand All @@ -352,20 +375,31 @@ func generateVersionFieldArray(version string, fields []ecsField, w io.Writer) e
func generateVersionIndex(aliases []versionAlias, w io.Writer) error {
f := jen.NewFile("version")

f.HeaderComment(licenseHeader)

f.PackageComment("Code generated by generator, DO NOT EDIT.")

latest := aliases[len(aliases)-1].Version
f.Var().Id("Latest").Op("=").Id("v" + strings.ReplaceAll(latest.String(), ".", "_"))

f.Var().Id("Index").Op("=").Map(jen.String()).Map(jen.String()).Id("*Field").CustomFunc(structListOptions, func(g *jen.Group) {
for _, a := range aliases {
g.Lit(a.Alias).Op(":").Id("v" + strings.ReplaceAll(a.Version.String(), ".", "_"))
}
f.Var().DefsFunc(func(g *jen.Group) {
// Latest = v1_2_3
g.Id("Latest").Op("=").Id(versionIdentifier(latest))

// Index = map[string]map[string]*Field{ ... }
g.Id("Index").Op("=").Map(jen.String()).Map(jen.String()).Id("*Field").
CustomFunc(structListOptions, func(g *jen.Group) {
for _, a := range aliases {
g.Lit(a.Alias).Op(":").Id(versionIdentifier(a.Version))
}
})
})

return f.Render(w)
}

func versionIdentifier(v *semver.Version) string {
return "v" + strings.ReplaceAll(v.String(), ".", "_")
}

func fieldsDefs(fields []ecsField, g *jen.Group) {
for _, f := range fields {
values := []jen.Code{
Expand Down
1 change: 1 addition & 0 deletions internal/generator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/coreos/go-semver v0.3.1
github.com/dave/jennifer v1.7.0
github.com/elastic/go-licenser v0.4.2-0.20230608174114-6eca7dbbc006
github.com/go-git/go-git/v5 v5.9.0
github.com/mitchellh/hashstructure v1.1.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 2 additions & 0 deletions internal/generator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/dave/jennifer v1.7.0/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elastic/go-licenser v0.4.2-0.20230608174114-6eca7dbbc006 h1:anF5YCbLmZOEXpE/2pwG1F0Jdy6s8tfKef4dKri4bkk=
github.com/elastic/go-licenser v0.4.2-0.20230608174114-6eca7dbbc006/go.mod h1:W8eH6FaZDR8fQGm+7FnVa7MxI1b/6dAqxz+zPB8nm5c=
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU=
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
Expand Down
126 changes: 64 additions & 62 deletions internal/version/index.go

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

Loading