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

Support step messages API #25

Merged
merged 1 commit into from
Mar 4, 2022
Merged
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
3 changes: 3 additions & 0 deletions client/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package client

//go:generate ./hack/scripts/generate
3 changes: 2 additions & 1 deletion client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/golangci/golangci-lint v1.42.0
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602
golang.org/x/tools v0.1.5
gotest.tools/gotestsum v1.7.0
)
3 changes: 1 addition & 2 deletions client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,8 @@ golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210313182246-cd4f82c27b84/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 h1:0Ja1LBD+yisY6RWM/BH7TJVXWsSjs2VwBSmvSX4HdBc=
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f h1:Qmd2pbz05z7z6lm0DrgQVVPuBm92jqujBKMHMOlOQEw=
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
21 changes: 21 additions & 0 deletions client/hack/scripts/generate
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
set -euo pipefail

RELAY_API_OPENAPI_URL="${RELAY_API_OPENAPI_URL:-"https://api.relay.sh/openapi/latest"}"

exec docker run \
--rm \
-v "$(dirname "$(go env GOMOD)"):/local" \
-w /local \
-v "$(go env GOROOT):/go" \
-e GO_POST_PROCESS_FILE="/go/bin/go run golang.org/x/tools/cmd/goimports -w" \
gcr.io/nebula-contrib/openapi-generator-cli:latest \
generate \
-i "${RELAY_API_OPENAPI_URL}" \
-g go \
-t /local/hack/templates \
--enable-post-process-file \
--global-property apis,models,supportingFiles,apiDocs=false,modelDocs=false \
--additional-properties useOneOfDiscriminatorLookup=true \
"$@" \
-o /local/pkg/client/openapi
111 changes: 111 additions & 0 deletions client/hack/templates/model_oneof.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// {{classname}} - {{{description}}}{{^description}}struct for {{{classname}}}{{/description}}
type {{classname}} struct {
{{#oneOf}}
{{{.}}} *{{{.}}}
{{/oneOf}}
}

{{#oneOf}}
// {{{.}}}As{{classname}} is a convenience function that returns {{{.}}} wrapped in {{classname}}
func {{{.}}}As{{classname}}(v *{{{.}}}) {{classname}} {
return {{classname}}{ {{{.}}}: v}
}

{{/oneOf}}

// Unmarshal JSON data into one of the pointers in the struct
func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
var err error
{{#isNullable}}
// this object is nullable so check if the payload is null or empty string
if string(data) == "" || string(data) == "{}" {
return nil
}

{{/isNullable}}
{{#useOneOfDiscriminatorLookup}}
{{#discriminator}}
{{#mappedModels}}
{{#-first}}
// use discriminator value to speed up the lookup
var jsonDict map[string]interface{}
err = json.Unmarshal(data, &jsonDict)
if err != nil {
return fmt.Errorf("Failed to unmarshal JSON into map for the discriminator lookup.")
}

{{/-first}}
// check if the discriminator value is '{{{mappingName}}}'
if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" {
// try to unmarshal JSON data into {{{modelName}}}
err = json.Unmarshal(data, &dst.{{{modelName}}})
if err == nil {
return nil // data stored in dst.{{{modelName}}}, return on the first match
} else {
dst.{{{modelName}}} = nil
return fmt.Errorf("Failed to unmarshal {{classname}} as {{{modelName}}}: %s", err.Error())
}
}

{{/mappedModels}}
{{/discriminator}}
{{/useOneOfDiscriminatorLookup}}

match := 0
{{#oneOf}}
// try to unmarshal data into {{{.}}}
err = json.Unmarshal(data, &dst.{{{.}}})
if err == nil {
json{{{.}}}, err := json.Marshal(dst.{{{.}}})
if err == nil {
if string(json{{{.}}}) == "" || string(json{{{.}}}) == "{}" { // empty struct
dst.{{{.}}} = nil
} else {
match++
}
} else {
dst.{{{.}}} = nil
}
} else {
dst.{{{.}}} = nil
}

{{/oneOf}}
if match > 1 { // more than 1 match
// reset to nil
{{#oneOf}}
dst.{{{.}}} = nil
{{/oneOf}}

return fmt.Errorf("Data matches more than one schema in oneOf({{classname}})")
} else if match == 1 {
return nil // exactly one match
} else { // no match
return fmt.Errorf("Data failed to match schemas in oneOf({{classname}})")
}
}

// Marshal data from the first non-nil pointers in the struct to JSON
func (src {{classname}}) MarshalJSON() ([]byte, error) {
{{#oneOf}}
if src.{{{.}}} != nil {
return json.Marshal(&src.{{{.}}})
}

{{/oneOf}}
return nil, nil // no data in oneOf schemas
}

// Get the actual instance
func (obj *{{classname}}) GetActualInstance() (interface{}) {
{{#oneOf}}
if obj.{{{.}}} != nil {
return obj.{{{.}}}
}

{{/oneOf}}
// all schemas are nil
return nil
}

{{>nullable_model}}
52 changes: 11 additions & 41 deletions client/pkg/client/openapi/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,21 @@ go.*
api/
docs/

# These models can only be generated when useOneOfDiscriminatorLookup=false, but
# don't behave correctly in practice because they use particular values of an
# object key to distinguish from one another, but that check isn't included in
# UnmarshalJSON() of the generated types.
model_revision_source.go

# These models are dependent upon types defined in the models above.
model_workflow_revision.go

# These APIs have conflicts across tags or dreaded "UNKNOWN_BASE_TYPE" structs
# (https://github.com/OpenAPITools/openapi-generator/issues/1229).
api_access_control.go
api_auth.go
api_billing.go
api_connection_providers.go
api_connections.go
api_revisions.go
api_terms.go
api_workflow_revisions.go
api_workflow_triggers.go
api_workflows.go

model_binary_string.go
model_binary_string_one_of.go
model_inline_response_200_1.go
model_inline_response_200_3.go
model_inline_response_200_4.go
model_inline_response_200_5.go
model_inline_response_200_6.go
model_inline_response_200_7.go
model_inline_response_200_8.go
model_inline_response_200_9.go
model_inline_response_200_10.go
model_inline_response_200_11.go
model_inline_response_200_12.go
model_inline_response_200_13.go
model_inline_response_200_14.go
model_inline_response_200_15.go
model_inline_response_200_16.go
model_revision_source.go
model_workflow.go
model_workflow_all_of.go
model_workflow_error.go
model_workflow_repository_source.go
model_workflow_revision_all_of.go
model_workflow_revision.go
model_workflow_source.go
model_workflow_summary_all_of.go
model_workflow_summary.go
model_workflow_state.go
model_workflow_trigger_all_of.go
model_workflow_trigger_composite_all_of.go
model_workflow_trigger_composite.go
model_workflow_trigger_source_state.go
model_workflow_trigger_source.go
model_workflow_trigger_state.go
model_workflow_trigger.go

README.md
55 changes: 55 additions & 0 deletions client/pkg/client/openapi/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
api_billing.go
api_events.go
api_notifications.go
api_revisions.go
api_subscriptions.go
api_terms.go
api_tokens.go
api_views.go
api_workflow_revisions.go
api_workflow_runs.go
api_workflow_secrets.go
api_workflow_triggers.go
client.go
configuration.go
model_accepted_terms.go
model_account.go
model_account_contact.go
model_account_entity.go
model_account_quotas.go
model_any_workflow_run_step_message.go
model_any_workflow_run_step_state.go
model_any_workflow_step.go
model_approval_workflow_run_step_state.go
model_approval_workflow_step.go
model_approval_workflow_step_all_of.go
model_auth0_auth.go
model_binary_string.go
model_binary_string_one_of.go
model_connection.go
model_connection_all_of.go
model_connection_auth_input.go
Expand Down Expand Up @@ -60,7 +68,23 @@ model_event_source_trigger.go
model_event_summary.go
model_event_workflow_run_creator.go
model_inline_response_200.go
model_inline_response_200_1.go
model_inline_response_200_10.go
model_inline_response_200_11.go
model_inline_response_200_12.go
model_inline_response_200_13.go
model_inline_response_200_14.go
model_inline_response_200_15.go
model_inline_response_200_16.go
model_inline_response_200_17.go
model_inline_response_200_2.go
model_inline_response_200_3.go
model_inline_response_200_4.go
model_inline_response_200_5.go
model_inline_response_200_6.go
model_inline_response_200_7.go
model_inline_response_200_8.go
model_inline_response_200_9.go
model_inline_response_default.go
model_invite.go
model_invite_all_of.go
Expand All @@ -69,6 +93,8 @@ model_invite_identifier.go
model_invite_summary.go
model_invite_summary_all_of.go
model_lifecycle.go
model_log_workflow_run_step_message.go
model_log_workflow_run_step_message_all_of.go
model_notification.go
model_notification_all_of.go
model_notification_entity.go
Expand All @@ -87,6 +113,14 @@ model_permission_grant.go
model_permission_identifier.go
model_permission_summary.go
model_permission_summary_all_of.go
model_plan.go
model_plan_all_of.go
model_plan_identifier.go
model_plan_price.go
model_plan_subscription.go
model_plan_subscription_all_of.go
model_plan_summary.go
model_plan_summary_all_of.go
model_profile_entity.go
model_push_workflow_trigger_source.go
model_push_workflow_trigger_source_state.go
Expand All @@ -107,6 +141,8 @@ model_secret_connection_auth_input.go
model_secret_connection_auth_status.go
model_secret_connection_provider_auth.go
model_session.go
model_spec_validation_workflow_run_step_message.go
model_spec_validation_workflow_run_step_message_all_of.go
model_terms.go
model_token.go
model_token_request.go
Expand Down Expand Up @@ -140,14 +176,21 @@ model_user_workflows_subscriptions.go
model_webhook_workflow_trigger_source.go
model_webhook_workflow_trigger_source_all_of.go
model_webhook_workflow_trigger_source_state.go
model_when_evaluation_workflow_run_step_message.go
model_when_evaluation_workflow_run_step_message_all_of.go
model_workflow.go
model_workflow_all_of.go
model_workflow_data_references.go
model_workflow_entity.go
model_workflow_error.go
model_workflow_identifier.go
model_workflow_output_reference.go
model_workflow_parameter.go
model_workflow_parameter_all_of.go
model_workflow_parameter_reference.go
model_workflow_relay_source.go
model_workflow_repository_source.go
model_workflow_revision_all_of.go
model_workflow_revision_identifier.go
model_workflow_revision_summary.go
model_workflow_run.go
Expand All @@ -166,6 +209,7 @@ model_workflow_run_step_decorator.go
model_workflow_run_step_decorator_all_of.go
model_workflow_run_step_decorator_link.go
model_workflow_run_step_decorator_link_link.go
model_workflow_run_step_message.go
model_workflow_run_step_output.go
model_workflow_run_step_output_state.go
model_workflow_run_step_state.go
Expand All @@ -178,9 +222,20 @@ model_workflow_secret.go
model_workflow_secret_summary.go
model_workflow_secret_value.go
model_workflow_secrets_summary.go
model_workflow_source.go
model_workflow_state.go
model_workflow_step.go
model_workflow_summary.go
model_workflow_summary_all_of.go
model_workflow_trigger.go
model_workflow_trigger_all_of.go
model_workflow_trigger_binding.go
model_workflow_trigger_composite.go
model_workflow_trigger_composite_all_of.go
model_workflow_trigger_identifier.go
model_workflow_trigger_source.go
model_workflow_trigger_source_state.go
model_workflow_trigger_state.go
model_workflow_trigger_summary.go
model_workflow_view.go
model_workflow_view_all_of.go
Expand Down
Loading