diff --git a/codegen.sh b/codegen.sh index 098af412..d378c6a9 100755 --- a/codegen.sh +++ b/codegen.sh @@ -15,46 +15,43 @@ OS="$(uname)" case "${OS}" in - 'Linux') - OS='linux' - SED_IFLAG=(-i'') - ;; - 'Darwin') - OS='macos' - SED_IFLAG=(-i '') - ;; - *) - echo "Operating system '${OS}' not supported." - exit 1 - ;; +'Linux') + OS='linux' + SED_IFLAG=(-i'') + ;; +'Darwin') + OS='macos' + SED_IFLAG=(-i '') + ;; +*) + echo "Operating system '${OS}' not supported." + exit 1 + ;; esac # Remove existing client generated code -mkdir -p tmp; -DIRS=( types client server ) -IGNORED_FILES=( README.md utils.go utils_test.go marshal_test.go account_currency.go account_coin.go ) - -for dir in "${DIRS[@]}" -do - rm -rf tmp/*; - for file in "${IGNORED_FILES[@]}" - do - [ -f "${dir:?}"/"${file:?}" ] && mv "${dir:?}"/"${file:?}" tmp; +mkdir -p tmp +DIRS=(types client server) +IGNORED_FILES=(README.md utils.go utils_test.go marshal_test.go account_currency.go account_coin.go) + +for dir in "${DIRS[@]}"; do + rm -rf tmp/* + for file in "${IGNORED_FILES[@]}"; do + [ -f "${dir:?}"/"${file:?}" ] && mv "${dir:?}"/"${file:?}" tmp done - rm -rf "${dir:?}"/*; + rm -rf "${dir:?}"/* - for file in "${IGNORED_FILES[@]}" - do - [ -f tmp/"${file:?}" ] && mv tmp/"${file:?}" "${dir:?}"/"${file:?}"; + for file in "${IGNORED_FILES[@]}"; do + [ -f tmp/"${file:?}" ] && mv tmp/"${file:?}" "${dir:?}"/"${file:?}" done done -rm -rf tmp; +rm -rf tmp # Download spec file from releases ROSETTA_SPEC_VERSION=1.4.12 -curl -L https://github.com/coinbase/rosetta-specifications/releases/download/v${ROSETTA_SPEC_VERSION}/api.json -o api.json; +curl -L https://github.com/coinbase/rosetta-specifications/releases/download/v${ROSETTA_SPEC_VERSION}/api.json -o api.json # Generate client + types code GENERATOR_VERSION=v4.3.0 @@ -63,23 +60,22 @@ docker run --user "$(id -u):$(id -g)" --rm -v "${PWD}":/local \ -i /local/api.json \ -g go \ -t /local/templates/client \ - --additional-properties packageName=client\ - -o /local/client_tmp; + --additional-properties packageName=client -o /local/client_tmp # Remove unnecessary client files -rm -f client_tmp/go.mod; -rm -f client_tmp/README.md; -rm -f client_tmp/go.mod; -rm -f client_tmp/go.sum; -rm -rf client_tmp/api; -rm -rf client_tmp/docs; -rm -f client_tmp/git_push.sh; -rm -f client_tmp/.travis.yml; -rm -f client_tmp/.gitignore; -rm -f client_tmp/.openapi-generator-ignore; -rm -rf client_tmp/.openapi-generator; -mv client_tmp/* client; -rm -rf client_tmp; +rm -f client_tmp/go.mod +rm -f client_tmp/README.md +rm -f client_tmp/go.mod +rm -f client_tmp/go.sum +rm -rf client_tmp/api +rm -rf client_tmp/docs +rm -f client_tmp/git_push.sh +rm -f client_tmp/.travis.yml +rm -f client_tmp/.gitignore +rm -f client_tmp/.openapi-generator-ignore +rm -rf client_tmp/.openapi-generator +mv client_tmp/* client +rm -rf client_tmp # Add server code docker run --user "$(id -u):$(id -g)" --rm -v "${PWD}":/local \ @@ -87,127 +83,123 @@ docker run --user "$(id -u):$(id -g)" --rm -v "${PWD}":/local \ -i /local/api.json \ -g go-server \ -t /local/templates/server \ - --additional-properties packageName=server\ - -o /local/server_tmp; + --additional-properties packageName=server -o /local/server_tmp # Remove unnecessary server files -rm -rf server_tmp/api; -rm -rf server_tmp/.openapi-generator; -rm -f server_tmp/.openapi-generator-ignore; -rm -f server_tmp/go.mod; -rm -f server_tmp/main.go; -rm -f server_tmp/README.md; -rm -f server_tmp/Dockerfile; -mv server_tmp/go/* server_tmp/.; -rm -rf server_tmp/go; +rm -rf server_tmp/api +rm -rf server_tmp/.openapi-generator +rm -f server_tmp/.openapi-generator-ignore +rm -f server_tmp/go.mod +rm -f server_tmp/main.go +rm -f server_tmp/README.md +rm -f server_tmp/Dockerfile +mv server_tmp/go/* server_tmp/. +rm -rf server_tmp/go rm -f server_tmp/model_*.go rm -f server_tmp/*_service.go -mv server_tmp/* server; -rm -rf server_tmp; +mv server_tmp/* server +rm -rf server_tmp # Remove spec file -rm -f api.json; +rm -f api.json # Fix linting issues -sed "${SED_IFLAG[@]}" 's/Api/API/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/Json/JSON/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/Id /ID /g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/Url/URL/g' client/* server/*; +sed "${SED_IFLAG[@]}" 's/Api/API/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/Json/JSON/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/Id /ID /g' client/* server/* +sed "${SED_IFLAG[@]}" 's/Url/URL/g' client/* server/* # Fix enum pointers -sed "${SED_IFLAG[@]}" 's/*CurveType/CurveType/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/*SignatureType/SignatureType/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/*CoinAction/CoinAction/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/*ExemptionType/ExemptionType/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/*BlockEventType/BlockEventType/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/*Direction/Direction/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/*Case/Case/g' client/* server/*; +sed "${SED_IFLAG[@]}" 's/*CurveType/CurveType/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/*SignatureType/SignatureType/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/*CoinAction/CoinAction/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/*ExemptionType/ExemptionType/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/*BlockEventType/BlockEventType/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/*Direction/Direction/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/*Case/Case/g' client/* server/* # Fix CurveTypes, SignatureTypes, CoinActions, ExemptionTypes, Direction, Case -sed "${SED_IFLAG[@]}" 's/SECP256K1/Secp256k1/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/SECP256R1/Secp256r1/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/EDWARDS25519/Edwards25519/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/TWEEDLE/Tweedle/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/ECDSA_RECOVERY/EcdsaRecovery/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/ECDSA/Ecdsa/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/ED25519/Ed25519/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/SCHNORR_1/Schnorr1/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/SCHNORR_POSEIDON/SchnorrPoseidon/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/PALLAS/Pallas/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/CREATED/CoinCreated/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/SPENT/CoinSpent/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/GREATER_OR_EQUAL/BalanceGreaterOrEqual/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/LESS_OR_EQUAL/BalanceLessOrEqual/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/DYNAMIC/BalanceDynamic/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/FORWARD/Forward/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/BACKWARD/Backward/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/UPPER_CASE/UpperCase/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/LOWER_CASE/LowerCase/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/CASE_SENSITIVE/CaseSensitive/g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/NULL/Null/g' client/* server/*; +sed "${SED_IFLAG[@]}" 's/SECP256K1/Secp256k1/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/SECP256R1/Secp256r1/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/EDWARDS25519/Edwards25519/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/TWEEDLE/Tweedle/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/ECDSA_RECOVERY/EcdsaRecovery/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/ECDSA/Ecdsa/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/ED25519/Ed25519/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/SCHNORR_1/Schnorr1/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/SCHNORR_POSEIDON/SchnorrPoseidon/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/PALLAS/Pallas/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/CREATED/CoinCreated/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/SPENT/CoinSpent/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/GREATER_OR_EQUAL/BalanceGreaterOrEqual/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/LESS_OR_EQUAL/BalanceLessOrEqual/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/DYNAMIC/BalanceDynamic/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/FORWARD/Forward/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/BACKWARD/Backward/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/UPPER_CASE/UpperCase/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/LOWER_CASE/LowerCase/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/CASE_SENSITIVE/CaseSensitive/g' client/* server/* +sed "${SED_IFLAG[@]}" 's/NULL/Null/g' client/* server/* # Convert HexBytes to Bytes -sed "${SED_IFLAG[@]}" '/Hex-encoded public key bytes in the format specified by the CurveType/d' client/* server/*; -sed "${SED_IFLAG[@]}" -E 's/HexBytes[[:space:]]+string/Bytes []byte/g' client/* server/*; +sed "${SED_IFLAG[@]}" '/Hex-encoded public key bytes in the format specified by the CurveType/d' client/* server/* +sed "${SED_IFLAG[@]}" -E 's/HexBytes[[:space:]]+string/Bytes []byte/g' client/* server/* # Remove special characters -sed "${SED_IFLAG[@]}" 's/`//g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/\"//g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/\<b>//g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/\<\/b>//g' client/* server/*; -sed "${SED_IFLAG[@]}" 's///g' client/* server/*; -sed "${SED_IFLAG[@]}" 's/<\/code>//g' client/* server/*; +sed "${SED_IFLAG[@]}" 's/`//g' client/* server/* +sed "${SED_IFLAG[@]}" 's/\"//g' client/* server/* +sed "${SED_IFLAG[@]}" 's/\<b>//g' client/* server/* +sed "${SED_IFLAG[@]}" 's/\<\/b>//g' client/* server/* +sed "${SED_IFLAG[@]}" 's///g' client/* server/* +sed "${SED_IFLAG[@]}" 's/<\/code>//g' client/* server/* # Fix slice containing pointers -sed "${SED_IFLAG[@]}" 's/\*\[\]/\[\]\*/g' client/* server/*; +sed "${SED_IFLAG[@]}" 's/\*\[\]/\[\]\*/g' client/* server/* # Fix map pointers -sed "${SED_IFLAG[@]}" 's/\*map/map/g' client/* server/*; +sed "${SED_IFLAG[@]}" 's/\*map/map/g' client/* server/* # Fix string array pointers -sed "${SED_IFLAG[@]}" 's/\[\]\*string/\[\]string/g' client/* server/*; +sed "${SED_IFLAG[@]}" 's/\[\]\*string/\[\]string/g' client/* server/* # Move model files to types/ -mv client/model_*.go types/; +mv client/model_*.go types/ for file in types/model_*.go; do - mv "$file" "${file/model_/}" + mv "$file" "${file/model_/}" done # Change model files to correct package -sed "${SED_IFLAG[@]}" 's/package client/package types/g' types/*; +sed "${SED_IFLAG[@]}" 's/package client/package types/g' types/* # Add version file -VERSION_CONTENTS=$(sed "s/VERSION/${ROSETTA_SPEC_VERSION}/g" < templates/types.txt); -echo "${VERSION_CONTENTS}" >> "types/types.go"; - +VERSION_CONTENTS=$(sed "s/VERSION/${ROSETTA_SPEC_VERSION}/g" >"types/types.go" # Inject Custom Marshaling Logic # shellcheck disable=SC2013 -for file in $(grep -Ril "hex_bytes" types) -do - if [[ "${file}" == *"_test.go"* ]];then - echo "Skipping injection for ${file}"; - continue; +for file in $(grep -Ril "hex_bytes" types); do + if [[ "${file}" == *"_test.go"* ]]; then + echo "Skipping injection for ${file}" + continue fi - RAW_NAME=$(echo "${file}" | cut -c7- | rev | cut -c4- | rev); - STRUCT_NAME=$(echo "${RAW_NAME}" | perl -pe 's/(?:\b|_)(\p{Ll})/\u$1/g'); - MARSHAL_CONTENTS=$(sed "s/STRUCT_NAME/${STRUCT_NAME}/g" < templates/marshal.txt); - echo "${MARSHAL_CONTENTS}" >> "${file}"; + RAW_NAME=$(echo "${file}" | cut -c7- | rev | cut -c4- | rev) + STRUCT_NAME=$(echo "${RAW_NAME}" | perl -pe 's/(?:\b|_)(\p{Ll})/\u$1/g') + MARSHAL_CONTENTS=$(sed "s/STRUCT_NAME/${STRUCT_NAME}/g" >"${file}" # shellcheck disable=SC1004 sed "${SED_IFLAG[@]}" 's/package types/package types\ import \(\ \"encoding\/hex\"\ \"encoding\/json\"\ - \)/g' "${file}"; + \)/g' "${file}" done # Override certain types with complex marshaling -OVERRIDDEN_TYPES=( signing_payload construction_derive_response construction_parse_response ) -for type in "${OVERRIDDEN_TYPES[@]}" -do - echo "Overriding ${type}"; - rm "types/${type}.go" && cp "templates/${type}.txt" "types/${type}.go"; +OVERRIDDEN_TYPES=(signing_payload construction_derive_response construction_parse_response) +for type in "${OVERRIDDEN_TYPES[@]}"; do + echo "Overriding ${type}" + rm "types/${type}.go" && cp "templates/${type}.txt" "types/${type}.go" done cp "templates/go.mod.types" "types/go.mod" @@ -216,7 +208,7 @@ cd .. # Format client generated code FORMAT_GEN="gofmt -w /local/types; gofmt -w /local/client; gofmt -w /local/server" -GOLANG_VERSION=1.18 +GOLANG_VERSION=1.19 docker run --rm -v "${PWD}":/local \ golang:${GOLANG_VERSION} sh -c \ "cd /local; make deps; ${FORMAT_GEN}; make add-license; make shorten-lines; make fix-imports; go mod tidy;" diff --git a/templates/go.mod.types b/templates/go.mod.types index c370901b..a634a255 100644 --- a/templates/go.mod.types +++ b/templates/go.mod.types @@ -1,3 +1,3 @@ module github.com/coinbase/rosetta-sdk-go/types -go 1.18 \ No newline at end of file +go 1.19 diff --git a/types/go.mod b/types/go.mod index 6b93da21..ea754603 100644 --- a/types/go.mod +++ b/types/go.mod @@ -1,5 +1,14 @@ module github.com/coinbase/rosetta-sdk-go/types -go 1.18 +go 1.19 -require github.com/mitchellh/mapstructure v1.5.0 +require ( + github.com/mitchellh/mapstructure v1.5.0 + github.com/stretchr/testify v1.9.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/types/go.sum b/types/go.sum index 59f4b8e6..bbde9e17 100644 --- a/types/go.sum +++ b/types/go.sum @@ -1,2 +1,12 @@ +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/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +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/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +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.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=