Skip to content

Commit

Permalink
Satisfy error and Validator interfaces for oneOf return types (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriehSchneier authored May 2, 2024
1 parent 7e48a22 commit 52734f3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
13 changes: 13 additions & 0 deletions codegen/arrai/auto/svc_types.arrai
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ let fieldJsonTag = \type \key
`::\i}
}

${cond {typename <: go.errorReturnTypes(app): $`
// Error fulfills the error interface.
func (s ${typename}) Error() string {
type plain ${typename}
return fmt.Sprintf("%+v", plain(s))
}
`}}

// *${typename} validator
func (s *${typename}) Validate() error {
return validator.Validate(s)
}

// Marshal data from the first non-nil pointer in the struct to JSON
func (dst ${typename}) MarshalJSON() ([]byte, error) {
${unionTypes >> $`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ import (
"testing"
"time"

"github.com/anz-bank/sysl-go/validator"
"rest_miscellaneous/internal/gen/pkg/servers/gateway"
"rest_miscellaneous/internal/gen/pkg/servers/gateway/array_response_backend"
"rest_miscellaneous/internal/gen/pkg/servers/gateway/encoder_backend"
"rest_miscellaneous/internal/gen/pkg/servers/gateway/multi_contenttype_backend"
"rest_miscellaneous/internal/gen/pkg/servers/gateway/oneof_backend"
"rest_miscellaneous/internal/gen/pkg/servers/gateway/types"

"github.com/anz-bank/sysl-go/common"
"github.com/anz-bank/sysl-go/core"
"github.com/sethvargo/go-retry"
"github.com/stretchr/testify/require"

"github.com/anz-bank/sysl-go/common"
"github.com/anz-bank/sysl-go/core"
)

const applicationConfig = `---
Expand Down Expand Up @@ -307,6 +309,21 @@ func TestMiscellaneous_OneOfRaw(t *testing.T) {
Send()
}

func TestMiscellaneous_OneOfErrorReturn(t *testing.T) {
t.Parallel()

// Just want to confirm that it generates types that satisfy the 'error' and 'Validator' interfaces
var e error
e = gateway.Post_rotateOneOf_400_resp_type_body{}
e = oneof_backend.Post_rotateOneOf_400_resp_type_body{}
_ = e

var v validator.Validator
v = &gateway.Post_rotateOneOf_400_resp_type_body{}
v = &oneof_backend.Post_rotateOneOf_400_resp_type_body{}
_ = v
}

func TestMiscellaneous_MultiCode(t *testing.T) {
t.Parallel()
gatewayTester := gateway.NewTestServer(t, context.Background(), createService, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ paths:
application/json; charset = utf-8:
schema:
$ref: "#/components/schemas/OneOfResponse"
'400':
description: error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/One"
- $ref: "#/components/schemas/Two"

components:
schemas:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ paths:
example:
values:
- one: true
'400':
description: error
content:
application/json:
schema:
oneOf:
- $ref: "#/components/schemas/One"
- $ref: "#/components/schemas/Two"

components:
schemas:
Expand Down

0 comments on commit 52734f3

Please sign in to comment.