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

move constants into proxy code file where they are used to prevent accidental overwriting #6

Merged
merged 5 commits into from
Feb 8, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
test:
strategy:
matrix:
go-version: [ 1.20.x, 1.21.x ]
go-version: [ 1.20.x, 1.21.x, 1.22.x ]
platform: [ ubuntu-latest, macos-latest, windows-latest ]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ jobs:
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
-
name: Docker Login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Disabled until we figure out docker hub publishing
# -
sgtsquiggs marked this conversation as resolved.
Show resolved Hide resolved
# name: Docker Login
# uses: docker/login-action@v1
# with:
# username: ${{ secrets.DOCKERHUB_USERNAME }}
# password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
# OS X
.DS_Store

.history
.history

/dist
75 changes: 42 additions & 33 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ before:
hooks:
- go mod download

universal_binaries:
- replace: true

builds:
- binary: reinforcer
main: ./cmd/reinforcer/main.go
Expand All @@ -16,17 +19,20 @@ builds:
- darwin
goarch:
- amd64
- arm64
ignore:
- goos: linux
goarch: arm
- goos: windows
goarch: arm64

archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
files:
- files:
- README.md
- LICENSE
format_overrides:
- goos: windows
format: zip

checksum:
name_template: "checksum.txt"
Expand All @@ -37,30 +43,33 @@ snapshot:
changelog:
sort: asc

dockers:
- goos: linux
goarch: amd64
ids:
- reinforcer
image_templates:
- 'clear-street/reinforcer:{{ .Tag }}'
- 'clear-street/reinforcer:v{{ .Major }}'
- 'clear-street/reinforcer:v{{ .Major }}.{{ .Minor }}'
- 'clear-street/reinforcer:latest'
build_flag_templates:
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.name={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--label=org.opencontainers.image.source={{.GitURL}}"
# Disabled until we figure out docker hub publishing
#dockers:
# - goos: linux
sgtsquiggs marked this conversation as resolved.
Show resolved Hide resolved
# goarch: amd64
# ids:
# - reinforcer
# image_templates:
# - 'clear-street/reinforcer:{{ .Tag }}'
# - 'clear-street/reinforcer:v{{ .Major }}'
# - 'clear-street/reinforcer:v{{ .Major }}.{{ .Minor }}'
# - 'clear-street/reinforcer:latest'
# build_flag_templates:
# - "--pull"
# - "--label=org.opencontainers.image.created={{.Date}}"
# - "--label=org.opencontainers.image.name={{.ProjectName}}"
# - "--label=org.opencontainers.image.revision={{.FullCommit}}"
# - "--label=org.opencontainers.image.version={{.Version}}"
# - "--label=org.opencontainers.image.source={{.GitURL}}"


brews:
- homepage: https://github.com/clear-street/reinforcer
description: "Codegen tool for easy adoption of resiliency patterns in Go"
tap:
owner: clear-street
name: homebrew-reinforcer
folder: Formula
test: |
system "#{bin}/reinforcer --version"
# Disabled until we figure out brew publishing
#brews:
# - homepage: https://github.com/clear-street/reinforcer
# description: "Codegen tool for easy adoption of resiliency patterns in Go"
# tap:
# owner: clear-street
# name: homebrew-reinforcer
# folder: Formula
# test: |
# system "#{bin}/reinforcer --version"
62 changes: 19 additions & 43 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,10 @@ type statement interface {
Statement() (*jen.Statement, error)
}

type fileMeta struct {
fileConfig *FileConfig
methods []*method.Method
}

// Generated contains the code generation out for all the processed types
type Generated struct {
// Common is the golang code that is shared across all generated types
Common string
// Constants is the golang code that holds constants for compile-time safe references to the proxied methods
sgtsquiggs marked this conversation as resolved.
Show resolved Hide resolved
Constants string
// Files is the golang code that was generated for every type that was processed
Files []*GeneratedFile
}
Expand All @@ -107,8 +100,6 @@ func Generate(cfg Config) (*Generated, error) {
Common: c,
}

var fileMethods []*fileMeta

for _, fileConfig := range cfg.Files {
methods := fileConfig.methods
s, err := generateFile(cfg.OutPkg, cfg.IgnoreNoReturnMethods, fileConfig, methods)
Expand All @@ -119,15 +110,8 @@ func Generate(cfg Config) (*Generated, error) {
TypeName: fileConfig.outTypeName,
Contents: s,
})
fileMethods = append(fileMethods, &fileMeta{fileConfig: fileConfig, methods: methods})
}

consts, err := generateConstants(cfg.OutPkg, fileMethods)
if err != nil {
return nil, err
}
gen.Constants = consts

return gen, nil
}

Expand All @@ -137,6 +121,25 @@ func generateFile(outPkg string, ignoreNoReturnMethods bool, fileCfg *FileConfig
f := jen.NewFile(outPkg)
f.HeaderComment(fileHeader)

// Compile-time constants
var fields []jen.Code
var constantAssign []jen.Code
for _, m := range methods {
fields = append(fields, jen.Id(m.Name).Id("string"))
constantAssign = append(constantAssign, jen.Id(m.Name).Op(":").Lit(m.Name).Op(","))
}

constObjName := fmt.Sprintf("%sMethods", fileCfg.outTypeName)
log.Debug().Msgf("Adding constants for type %s", fileCfg.outTypeName)
f.Add(jen.Comment(fmt.Sprintf("%s are the methods in %s", constObjName, fileCfg.outTypeName)))
f.Add(
jen.Var().Id(constObjName).Op("=").Struct(
fields...,
).Block(
constantAssign...,
),
)

// Declare the target interface we are proxying
var declMethods []jen.Code
for _, meth := range methods {
Expand Down Expand Up @@ -251,33 +254,6 @@ func generateCommon(outPkg string) (string, error) {
return renderToString(f)
}

func generateConstants(outPkg string, meta []*fileMeta) (string, error) {
f := jen.NewFile(outPkg)
f.HeaderComment(fileHeader)

for _, fm := range meta {
var fields []jen.Code
var constantAssign []jen.Code
for _, m := range fm.methods {
fields = append(fields, jen.Id(m.Name).Id("string"))
constantAssign = append(constantAssign, jen.Id(m.Name).Op(":").Lit(m.Name).Op(","))
}

constObjName := fmt.Sprintf("%sMethods", fm.fileConfig.outTypeName)
log.Debug().Msgf("Adding constants for type %s", fm.fileConfig.outTypeName)
f.Add(jen.Comment(fmt.Sprintf("%s are the methods in %s", constObjName, fm.fileConfig.outTypeName)))
f.Add(
jen.Var().Id(constObjName).Op("=").Struct(
fields...,
).Block(
constantAssign...,
),
)
}

return renderToString(f)
}

func renderToString(f *jen.File) (string, error) {
b := &bytes.Buffer{}
if err := f.Render(b); err != nil {
Expand Down
Loading
Loading