From b930d21384358d86ec9ec6f06fec403ade9119f0 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 22 Aug 2023 15:55:43 +0200 Subject: [PATCH] feat: support function route `methods` field --- go/models/function_route.go | 54 ++++++++++++++++++++++++++++++ go/porcelain/deploy.go | 1 + go/porcelain/deploy_test.go | 4 ++- go/porcelain/functions_manifest.go | 7 ++-- swagger.yml | 7 +++- 5 files changed, 68 insertions(+), 5 deletions(-) diff --git a/go/models/function_route.go b/go/models/function_route.go index db4368d0..49973718 100644 --- a/go/models/function_route.go +++ b/go/models/function_route.go @@ -6,8 +6,13 @@ package models // Editing this file might prove futile when you re-run the swagger generate command import ( + "encoding/json" + "strconv" + + "github.com/go-openapi/errors" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" + "github.com/go-openapi/validate" ) // FunctionRoute function route @@ -21,12 +26,61 @@ type FunctionRoute struct { // literal Literal string `json:"literal,omitempty"` + // methods + Methods []string `json:"methods"` + // pattern Pattern string `json:"pattern,omitempty"` } // Validate validates this function route func (m *FunctionRoute) Validate(formats strfmt.Registry) error { + var res []error + + if err := m.validateMethods(formats); err != nil { + res = append(res, err) + } + + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +var functionRouteMethodsItemsEnum []interface{} + +func init() { + var res []string + if err := json.Unmarshal([]byte(`["GET","POST","PUT","PATCH","DELETE","OPTIONS"]`), &res); err != nil { + panic(err) + } + for _, v := range res { + functionRouteMethodsItemsEnum = append(functionRouteMethodsItemsEnum, v) + } +} + +func (m *FunctionRoute) validateMethodsItemsEnum(path, location string, value string) error { + if err := validate.EnumCase(path, location, value, functionRouteMethodsItemsEnum, true); err != nil { + return err + } + return nil +} + +func (m *FunctionRoute) validateMethods(formats strfmt.Registry) error { + + if swag.IsZero(m.Methods) { // not required + return nil + } + + for i := 0; i < len(m.Methods); i++ { + + // value enum + if err := m.validateMethodsItemsEnum("methods"+"."+strconv.Itoa(i), "body", m.Methods[i]); err != nil { + return err + } + + } + return nil } diff --git a/go/porcelain/deploy.go b/go/porcelain/deploy.go index 8fc74426..36c81b63 100644 --- a/go/porcelain/deploy.go +++ b/go/porcelain/deploy.go @@ -799,6 +799,7 @@ func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer Dep Pattern: route.Pattern, Literal: route.Literal, Expression: route.Expression, + Methods: route.Methods, } } diff --git a/go/porcelain/deploy_test.go b/go/porcelain/deploy_test.go index d404307e..a5de80d2 100644 --- a/go/porcelain/deploy_test.go +++ b/go/porcelain/deploy_test.go @@ -480,7 +480,8 @@ func TestBundleWithManifest(t *testing.T) { }, { "pattern": "/products/:id", - "expression": "^/products/(.*)$" + "expression": "^/products/(.*)$", + "methods": ["GET", "POST"] } ] }, @@ -525,6 +526,7 @@ func TestBundleWithManifest(t *testing.T) { assert.Equal(t, "/products/:id", helloJSConfig.Routes[1].Pattern) assert.Empty(t, helloJSConfig.Routes[1].Literal) assert.Equal(t, "^/products/(.*)$", helloJSConfig.Routes[1].Expression) + assert.Equal(t, []string{"GET", "POST"}, helloJSConfig.Routes[1].Methods) } func TestReadZipRuntime(t *testing.T) { diff --git a/go/porcelain/functions_manifest.go b/go/porcelain/functions_manifest.go index a2dda3e8..7e5a67fc 100644 --- a/go/porcelain/functions_manifest.go +++ b/go/porcelain/functions_manifest.go @@ -20,7 +20,8 @@ type functionsManifestEntry struct { } type functionRoute struct { - Pattern string `json:"pattern"` - Literal string `json:"literal"` - Expression string `json:"expression"` + Pattern string `json:"pattern"` + Literal string `json:"literal"` + Expression string `json:"expression"` + Methods []string `json:"methods"` } diff --git a/swagger.yml b/swagger.yml index e981f0b6..45e9de09 100644 --- a/swagger.yml +++ b/swagger.yml @@ -3632,7 +3632,12 @@ definitions: literal: type: string expression: - type: string + type: string + methods: + type: array + items: + type: string + enum: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] parameters: page: type: integer