-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: rewrites generate script to use cli-supported codemod configs …
…like the name normalizer output option and oapi overlay spec file
- Loading branch information
Showing
4 changed files
with
164 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.