Skip to content

Commit

Permalink
build: rewrites generate script to use cli-supported codemod configs …
Browse files Browse the repository at this point in the history
…like the name normalizer output option and oapi overlay spec file
  • Loading branch information
ctran88 committed Dec 23, 2024
1 parent 5fa5d16 commit ad5ba71
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 103 deletions.
9 changes: 9 additions & 0 deletions cfg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package: passage
generate:
client: true
models: true
output: passage.gen.go
output-options:
name-normalizer: ToCamelCaseWithInitialisms
overlay:
path: overlay.yml
84 changes: 7 additions & 77 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -1,84 +1,14 @@
# #!/bin/bash
set -euo pipefail

output_file="passage.gen.go"
if [ -z "$1" ]; then
echo "Required generator file is missing."
exit 1
fi

# Skip pointer on optional fields
fields="\
.components.schemas.CreateUserRequest.properties.email \
.components.schemas.CreateUserRequest.properties.phone \
.components.schemas.CreateUserRequest.properties.user_metadata \
.components.schemas.UpdateUserRequest.properties.email \
.components.schemas.UpdateUserRequest.properties.phone \
.components.schemas.UpdateUserRequest.properties.user_metadata \
.components.schemas.CreateMagicLinkRequest.properties.channel \
.components.schemas.CreateMagicLinkRequest.properties.email \
.components.schemas.CreateMagicLinkRequest.properties.language \
.components.schemas.CreateMagicLinkRequest.properties.magic_link_path \
.components.schemas.CreateMagicLinkRequest.properties.phone \
.components.schemas.CreateMagicLinkRequest.properties.redirect_url \
.components.schemas.CreateMagicLinkRequest.properties.send \
.components.schemas.CreateMagicLinkRequest.properties.ttl \
.components.schemas.CreateMagicLinkRequest.properties.type \
.components.schemas.CreateMagicLinkRequest.properties.user_id \
.components.schemas.MagicLinkType \
.components.schemas.MagicLinkChannel \
.components.schemas.UserInfo.properties.user_metadata \
.components.schemas.UserInfo.properties.webauthn_types"
for field in $fields; do
jq "$field |= . + {\"x-go-type-skip-optional-pointer\": true}" openapi.json > tmp.json && mv tmp.json openapi.json
done

# Rename component to avoid name clash with generated struct
jq ".components.schemas.ListPaginatedUsersResponse |= . + {\"x-go-name\": \"PaginatedUsersResponse\"}" openapi.json > tmp.json && mv tmp.json openapi.json

# JSON string of key-value pairs
transforms='{
"Active": "StatusActive",
"CreateMagicLinkRequest": "CreateMagicLinkBody",
"CreateUserRequest": "CreateUserBody",
"Inactive": "StatusInactive",
"MagicLinkTypeLogin": "LoginType",
"MagicLinkTypeVerifyIdentifier": "VerifyIdentifierType",
"MagicLinkChannel": "ChannelType",
"MagicLinkChannelEmail": "EmailChannel",
"MagicLinkChannelPhone": "PhoneChannel",
"Pending": "StatusPending",
"UpdateUserRequest": "UpdateBody",
"UserInfo": "User",
"UserMetadataFieldTypeBoolean": "BooleanMD",
"UserMetadataFieldTypeDate": "DateMD",
"UserMetadataFieldTypeEmail": "EmailMD",
"UserMetadataFieldTypeInteger": "IntegerMD",
"UserMetadataFieldTypePhone": "PhoneMD",
"UserMetadataFieldTypeString": "StringMD"
}'

# Function to perform replacements
command -v gorename &> /dev/null || go install golang.org/x/tools/cmd/[email protected]
replace() {
local in=$1 out=$2
gorename -from "\"github.com/passageidentity/passage-go\".$in" -to "$out" -force
}

# Un-apply transforms
echo "$transforms" | jq -r 'to_entries[] | "\(.key) \(.value)"' | while read -r key value; do
replace "$value" "$key"
done
file="$1"

# Run codegen
go run github.com/oapi-codegen/oapi-codegen/v2/cmd/[email protected] \
-generate types,client \
-package passage \
-o "$output_file" \
-initialism-overrides \
openapi.json

# Replace initialisms with uppercase versions
perl -pe 's/(Url|Uri|Ttl|Id|Rsa|Ip)(s?)(?=\b|[A-Z])/\U$1\E$2/g' "$output_file" > tmp.txt && mv tmp.txt "$output_file"

# Apply transforms
echo "$transforms" | jq -r 'to_entries[] | "\(.key) \(.value)"' | while read -r key value; do
replace "$key" "$value"
done

-config cfg.yml \
$file
122 changes: 122 additions & 0 deletions overlay.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
overlay: 1.0.0
info:
title: "passage-go-specific codegen updates via the OpenAPI Overlay specification (https://github.com/OAI/Overlay-Specification)"
version: 1.0.0
actions:

# rename types

- target: $.components.schemas.CreateUserRequest
description: Rename the generated Go type name
update:
x-go-name: CreateUserArgs

- target: $.components.schemas.UpdateUserRequest
description: Rename the generated Go type name
update:
x-go-name: UpdateUserOptions

- target: $.components.schemas.ListPaginatedUsersResponse
description: Rename the generated Go type name
update:
x-go-name: PaginatedUsersResponse

- target: $.components.schemas.UserInfo
description: Rename the generated Go type name
update:
x-go-name: PassageUser

- target: $.components.schemas.MagicLinkChannel
description: Rename the generated Go type name
update:
x-go-name: ChannelType

- target: $.components.schemas.CreateMagicLinkRequest
description: Rename the generated Go type name and make it private
update:
x-go-name: magicLinkArgs

- target: $.components.schemas.ListPaginatedUsersResponse
description: Rename the generated Go type name and make it private
update:
x-go-name: paginatedUsersResponse

# rename enums

- target: $.components.schemas.UserStatus
description: Rename the generated Go enum name
update:
x-enumNames:
- StatusActive
- StatusInactive
- StatusPending

- target: $.components.schemas.MagicLinkType
description: Rename the generated Go enum name
update:
x-enumNames:
- LoginType
- VerifyIdentifierType

- target: $.components.schemas.MagicLinkChannel
description: Rename the generated Go enum name
update:
x-enumNames:
- EmailChannel
- PhoneChannel

- target: $.components.schemas.UserMetadataFieldType
description: Rename the generated Go enum name
update:
x-enumNames:
- StringMD
- BooleanMD
- IntegerMD
- DateMD
- PhoneMD
- EmailMD

- target: $.components.schemas.UserEventAction
description: Rename the generated Go enum name
update:
x-enumNames:
- UserEventActionRegister
- UserEventActionLogin
- UserEventActionOther

# skip optional pointers

- target: $.components.schemas.CreateUserRequest.properties.*
description: Don't use pointers for optional properties in the generated Go type
update:
x-go-type-skip-optional-pointer: true

- target: $.components.schemas.UpdateUserRequest.properties.*
description: Don't use pointers for optional properties in the generated Go type
update:
x-go-type-skip-optional-pointer: true

- target: $.components.schemas.CreateMagicLinkRequest.properties.*
description: Don't use pointers for optional properties in the generated Go type
update:
x-go-type-skip-optional-pointer: true

- target: $.components.schemas.MagicLinkType
description: Don't use pointers for optional properties in the generated Go type
update:
x-go-type-skip-optional-pointer: true

- target: $.components.schemas.MagicLinkChannel
description: Don't use pointers for optional properties in the generated Go type
update:
x-go-type-skip-optional-pointer: true

- target: $.components.schemas.UserInfo.properties.user_metadata
description: Don't use pointers for optional properties in the generated Go type
update:
x-go-type-skip-optional-pointer: true

- target: $.components.schemas.UserInfo.properties.webauthn_types
description: Don't use pointers for optional properties in the generated Go type
update:
x-go-type-skip-optional-pointer: true
52 changes: 26 additions & 26 deletions passage.gen.go

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

0 comments on commit ad5ba71

Please sign in to comment.