Skip to content

Instantly share code, notes, and snippets.

@syall
Last active February 8, 2023 06:49
Show Gist options
  • Save syall/9048349e07fcdd15427685633b5843ec to your computer and use it in GitHub Desktop.
Save syall/9048349e07fcdd15427685633b5843ec to your computer and use it in GitHub Desktop.
smithy-go `go.mod` generation

smithy-go go.mod generation

Overview

smithy-go currently does not generate go.mod, go.sum, or go_module_metadata.go files, but it probably should.

There are example artfacts attached in this gist:

  • go.mod
  • go.sum
  • go_module_metadata.go

The common configurations of a go.mod file include:

  • module $MODULE_NAME
  • go $MINIMUM_GO_VERSION
  • const goModuleVersion = $MODULE_VERSION
  • require ( $DEPENDENCY_NAME $DEPENDENCY_VERSION ... ) (handled by smithy-go)

All module-aware commands can be found in the Go Module Reference.

go.mod and go.sum

Currently the AWS SDK for Go v2 uses the gomodgen tool to generate go.mod with the manifest generated.json file by using the mod/modfile package to directly control the AST of a go.mod file.

Potential solutions to generate go.mod:

  • Use the go mod commands to create the go.mod file (Recommended)
    • Idiomatic way of creating go.mod files
    • This requires a dependency on go during the Java code generation step
    • Example commands:
      • go mod init $MODULE_NAME
      • go mod edit -require=$DEPENDENCY_NAME@$DEPENDENCY_VERSION
      • go mod edit -go=$MINIMUM_GO_VERSION
  • Implement a gomodgen-like tool to generate a go.mod file
    • This requires a dependency on go during the Java code generation step
  • Use the existing gomodgen tool to transform a generated.json file into a go.mod file
    • This requires a dependency on go during the Java code generation step
    • This requires a dependency on aws-go-multi-module-repository-tools
    • generated.json is a custom intermediate format, not considered idiomatic
  • Implement a custom go.mod writer in Java
    • Manually writing go.mod files in Java is not considered idiomatic

go.sum files should only be generated by running go mod tidy after go.mod is generated.

go_module_metadata.go

Currently the AWS SDK for Go v2 uses the updatemodulemeta tool to generatego_module_metadata.go based on git tags.

Potential solutions to generate go_module_metadata.go:

  • Implement a custom go_module_metadata.go writer in Java (Recommended)
    • This does NOT requires a dependency on git tags as a release mechanism
    • Writing Go code is built-in for smithy-go, low complexity
  • Implement a updatemodulemeta-like tool to generate go_module_metadata.go
    • This requires a dependency on go during the Java code generation step
  • Use the existing updatemodulemeta tool to generate go_module_metadata.go
    • This requires a dependency on go during the Java code generation step
    • This requires a dependency on aws-go-multi-module-repository-tools
    • This requires a dependency on git tags as a release mechanism

Running go in smithy-go

Using go in smithy-go could lead to dependency and platform complexity.

Although there are uses of go in smithy-go, such as TestUtils.java, it is not common in the code generation workflows.

A go command would eventually need to be used when generated go.sum through go mod tidy.

References

module github.com/aws/smithy-go/tests/service/weather
go 1.15
require (
github.com/aws/smithy-go v1.13.5
github.com/jmespath/go-jmespath v0.4.0
)
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
package weather
const goModuleVersion = "0.0.1"
@syall
Copy link
Author

syall commented Feb 8, 2023

Opt-in go.mod generation via go commands is implemented in these two merged PRs:

The PRs do not include generating go.sum and go_module_metadata.go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment