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

build: rewrite generate script to use cli-supported codemod configs #121

Open
wants to merge 2 commits into
base: PSG-5216-remove-deprecated-code
Choose a base branch
from
Open
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
32 changes: 16 additions & 16 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func (a *auth) CreateMagicLinkWithEmail(
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
Email: email,
Channel: EmailChannel,
Type: magicLinkType,
Send: send,
args := magicLinkArgs{
Email: email,
ChannelType: EmailChannel,
Type: magicLinkType,
Send: send,
}

return a.createMagicLink(args, opts)
Expand All @@ -67,11 +67,11 @@ func (a *auth) CreateMagicLinkWithPhone(
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
Phone: phone,
Channel: PhoneChannel,
Type: magicLinkType,
Send: send,
args := magicLinkArgs{
Phone: phone,
ChannelType: PhoneChannel,
Type: magicLinkType,
Send: send,
}

return a.createMagicLink(args, opts)
Expand All @@ -85,11 +85,11 @@ func (a *auth) CreateMagicLinkWithUser(
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
UserID: userID,
Channel: channel,
Type: magicLinkType,
Send: send,
args := magicLinkArgs{
UserID: userID,
ChannelType: channel,
Type: magicLinkType,
Send: send,
}

return a.createMagicLink(args, opts)
Expand Down Expand Up @@ -123,7 +123,7 @@ func (a *auth) ValidateJWT(jwt string) (string, error) {
return userID, nil
}

func (a *auth) createMagicLink(args CreateMagicLinkBody, opts *MagicLinkOptions) (*MagicLink, error) {
func (a *auth) createMagicLink(args magicLinkArgs, opts *MagicLinkOptions) (*MagicLink, error) {
if opts != nil {
args.Language = opts.Language
args.MagicLinkPath = opts.MagicLinkPath
Expand Down
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
Loading
Loading