bcupdate: add service.Match, parameter.assignDataType, service.matchPattern, server.collide
continuous-integration/drone/push Build is failing Details

- datatype-s are required as arguments in Parse(), datatypes are built into the config parameters
 - collision detection compares : http method, pattern (both fixed, one/both captures)
 - test service.Match(); more to test
 - some refactor and fix tests
This commit is contained in:
Adrien Marquès 2020-03-15 00:27:54 +01:00
parent 6a144a9a93
commit 32aff3e07f
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
7 changed files with 366 additions and 173 deletions

View File

@ -3,8 +3,12 @@ package config
import ( import (
"errors" "errors"
"fmt" "fmt"
"net/http"
"net/http/httptest"
"strings" "strings"
"testing" "testing"
"git.xdrm.io/go/aicra/config/datatype/builtin"
) )
func TestLegalServiceName(t *testing.T) { func TestLegalServiceName(t *testing.T) {
@ -219,22 +223,22 @@ func TestParamEmptyRenameNoRename(t *testing.T) {
"path": "/", "path": "/",
"info": "valid-description", "info": "valid-description",
"in": { "in": {
"original": { "info": "valid-desc", "type": "valid-type", "name": "" } "original": { "info": "valid-desc", "type": "any", "name": "" }
} }
} }
]`) ]`)
srv, err := Parse(reader) srv, err := Parse(reader, builtin.AnyDataType{})
if err != nil { if err != nil {
t.Errorf("unexpected error: '%s'", err) t.Errorf("unexpected error: '%s'", err)
t.FailNow() t.FailNow()
} }
if len(srv) < 1 { if len(srv.services) < 1 {
t.Errorf("expected a service") t.Errorf("expected a service")
t.FailNow() t.FailNow()
} }
for _, param := range (srv)[0].Input { for _, param := range srv.services[0].Input {
if param.Rename != "original" { if param.Rename != "original" {
t.Errorf("expected the parameter 'original' not to be renamed to '%s'", param.Rename) t.Errorf("expected the parameter 'original' not to be renamed to '%s'", param.Rename)
t.FailNow() t.FailNow()
@ -249,24 +253,24 @@ func TestOptionalParam(t *testing.T) {
"path": "/", "path": "/",
"info": "valid-description", "info": "valid-description",
"in": { "in": {
"optional": { "info": "valid-desc", "type": "?optional-type" }, "optional": { "info": "optional-type", "type": "?bool" },
"required": { "info": "valid-desc", "type": "required-type" }, "required": { "info": "required-type", "type": "bool" },
"required2": { "info": "valid-desc", "type": "a" }, "required2": { "info": "required", "type": "any" },
"optional2": { "info": "valid-desc", "type": "?a" } "optional2": { "info": "optional", "type": "?any" }
} }
} }
]`) ]`)
srv, err := Parse(reader) srv, err := Parse(reader, builtin.AnyDataType{}, builtin.BoolDataType{})
if err != nil { if err != nil {
t.Errorf("unexpected error: '%s'", err) t.Errorf("unexpected error: '%s'", err)
t.FailNow() t.FailNow()
} }
if len(srv) < 1 { if len(srv.services) < 1 {
t.Errorf("expected a service") t.Errorf("expected a service")
t.FailNow() t.FailNow()
} }
for pName, param := range (srv)[0].Input { for pName, param := range srv.services[0].Input {
if pName == "optional" || pName == "optional2" { if pName == "optional" || pName == "optional2" {
if !param.Optional { if !param.Optional {
@ -389,7 +393,7 @@ func TestParseParameters(t *testing.T) {
"path": "/", "path": "/",
"info": "info", "info": "info",
"in": { "in": {
"param1": { "info": "valid", "type": "a" } "param1": { "info": "valid", "type": "any" }
} }
} }
]`, ]`,
@ -402,7 +406,7 @@ func TestParseParameters(t *testing.T) {
"path": "/", "path": "/",
"info": "info", "info": "info",
"in": { "in": {
"param1": { "info": "valid", "type": "?valid" } "param1": { "info": "valid", "type": "?any" }
} }
} }
]`, ]`,
@ -416,8 +420,8 @@ func TestParseParameters(t *testing.T) {
"path": "/", "path": "/",
"info": "info", "info": "info",
"in": { "in": {
"param1": { "info": "valid", "type": "valid" }, "param1": { "info": "valid", "type": "any" },
"param2": { "info": "valid", "type": "valid", "name": "param1" } "param2": { "info": "valid", "type": "any", "name": "param1" }
} }
} }
]`, ]`,
@ -431,8 +435,8 @@ func TestParseParameters(t *testing.T) {
"path": "/", "path": "/",
"info": "info", "info": "info",
"in": { "in": {
"param1": { "info": "valid", "type": "valid", "name": "param2" }, "param1": { "info": "valid", "type": "any", "name": "param2" },
"param2": { "info": "valid", "type": "valid" } "param2": { "info": "valid", "type": "any" }
} }
} }
]`, ]`,
@ -446,8 +450,8 @@ func TestParseParameters(t *testing.T) {
"path": "/", "path": "/",
"info": "info", "info": "info",
"in": { "in": {
"param1": { "info": "valid", "type": "valid", "name": "conflict" }, "param1": { "info": "valid", "type": "any", "name": "conflict" },
"param2": { "info": "valid", "type": "valid", "name": "conflict" } "param2": { "info": "valid", "type": "any", "name": "conflict" }
} }
} }
]`, ]`,
@ -462,8 +466,8 @@ func TestParseParameters(t *testing.T) {
"path": "/", "path": "/",
"info": "info", "info": "info",
"in": { "in": {
"param1": { "info": "valid", "type": "valid", "name": "freename" }, "param1": { "info": "valid", "type": "any", "name": "freename" },
"param2": { "info": "valid", "type": "valid", "name": "freename2" } "param2": { "info": "valid", "type": "any", "name": "freename2" }
} }
} }
]`, ]`,
@ -474,7 +478,7 @@ func TestParseParameters(t *testing.T) {
for i, test := range tests { for i, test := range tests {
t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) { t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) {
_, err := Parse(strings.NewReader(test.Raw)) _, err := Parse(strings.NewReader(test.Raw), builtin.AnyDataType{})
if err == nil && test.Error != nil { if err == nil && test.Error != nil {
t.Errorf("expected an error: '%s'", test.Error.Error()) t.Errorf("expected an error: '%s'", test.Error.Error())
@ -497,136 +501,141 @@ func TestParseParameters(t *testing.T) {
} }
// todo: rewrite with new api format // todo: rewrite with new api format
// func TestMatchSimple(t *testing.T) { func TestMatchSimple(t *testing.T) {
// tests := []struct { tests := []struct {
// Raw string Config string
// Path []string URL string
// BrowseDepth int Match bool
// ValidDepth bool }{
// }{ { // false positive -1
// { // false positive -1 `[ {
// `{ "method": "GET",
// "/" : { "path": "/a",
// "parent": { "info": "info",
// "/": { "in": {}
// "subdir": {} } ]`,
// } "/",
// } false,
// } },
// }`, { // false positive +1
// []string{"parent", "subdir"}, `[ {
// 1, "method": "GET",
// false, "path": "/",
// }, "info": "info",
// { // false positive +1 "in": {}
// `{ } ]`,
// "/" : { "/a",
// "parent": { false,
// "/": { },
// "subdir": {} {
// } `[ {
// } "method": "GET",
// } "path": "/a",
// }`, "info": "info",
// []string{"parent", "subdir"}, "in": {}
// 3, } ]`,
// false, "/a",
// }, true,
},
{
`[ {
"method": "GET",
"path": "/a",
"info": "info",
"in": {}
} ]`,
"/a/",
true,
},
{
`[ {
"method": "GET",
"path": "/a/{id}",
"info": "info",
"in": {
"{id}": {
"info": "info",
"type": "bool"
}
}
} ]`,
"/a/12/",
false,
},
{
`[ {
"method": "GET",
"path": "/a/{id}",
"info": "info",
"in": {
"{id}": {
"info": "info",
"type": "int"
}
}
} ]`,
"/a/12/",
true,
},
{
`[ {
"method": "GET",
"path": "/a/{valid}",
"info": "info",
"in": {
"{id}": {
"info": "info",
"type": "bool"
}
}
} ]`,
"/a/12/",
false,
},
{
`[ {
"method": "GET",
"path": "/a/{valid}",
"info": "info",
"in": {
"{id}": {
"info": "info",
"type": "bool"
}
}
} ]`,
"/a/true/",
true,
},
}
// { for i, test := range tests {
// `{
// "/" : {
// "parent": {
// "/": {
// "subdir": {}
// }
// }
// }
// }`,
// []string{"parent", "subdir"},
// 2,
// true,
// },
// { // unknown path
// `{
// "/" : {
// "parent": {
// "/": {
// "subdir": {}
// }
// }
// }
// }`,
// []string{"x", "y"},
// 2,
// false,
// },
// { // unknown path
// `{
// "/" : {
// "parent": {
// "/": {
// "subdir": {}
// }
// }
// }
// }`,
// []string{"parent", "y"},
// 1,
// true,
// },
// { // Warning: this case is important to understand the precedence of service paths over
// // the value of some variables. Here if we send a string parameter in the GET method that
// // unfortunately is equal to 'subdir', it will call the sub-service /parent/subdir' instead
// // of the service /parent with its parameter set to the value 'subdir'.
// `{
// "/" : {
// "parent": {
// "/": {
// "subdir": {}
// },
// "GET": {
// "info": "valid-desc",
// "in": {
// "some-value": {
// "info": "valid-desc",
// "type": "valid-type"
// }
// }
// }
// }
// }
// }`,
// []string{"parent", "subdir"},
// 2,
// true,
// },
// }
// for i, test := range tests { t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) {
srv, err := Parse(strings.NewReader(test.Config), builtin.AnyDataType{}, builtin.IntDataType{}, builtin.BoolDataType{})
// t.Run(fmt.Sprintf("method.%d", i), func(t *testing.T) { if err != nil {
// srv, err := Parse(strings.NewReader(test.Raw)) t.Errorf("unexpected error: '%s'", err)
t.FailNow()
}
// if err != nil { if len(srv.services) != 1 {
// t.Errorf("unexpected error: '%s'", err) t.Errorf("expected to have 1 service, got %d", len(srv.services))
// t.FailNow() t.FailNow()
// } }
// _, depth := srv.Match(test.Path) req := httptest.NewRequest(http.MethodGet, test.URL, nil)
// if test.ValidDepth {
// if depth != test.BrowseDepth {
// t.Errorf("expected a depth of %d (got %d)", test.BrowseDepth, depth)
// t.FailNow()
// }
// } else {
// if depth == test.BrowseDepth {
// t.Errorf("expected a depth NOT %d (got %d)", test.BrowseDepth, depth)
// t.FailNow()
// }
// } match := srv.services[0].Match(req)
// }) if test.Match && !match {
// } t.Errorf("expected '%s' to match", test.URL)
t.FailNow()
}
if !test.Match && match {
t.Errorf("expected '%s' NOT to match", test.URL)
t.FailNow()
}
})
}
// } }

View File

@ -38,6 +38,9 @@ const ErrMissingDescription = Error("missing description")
// ErrMissingParamDesc - a parameter is missing its description // ErrMissingParamDesc - a parameter is missing its description
const ErrMissingParamDesc = Error("missing parameter description") const ErrMissingParamDesc = Error("missing parameter description")
// ErrUnknownDataType - a parameter has an unknown datatype name
const ErrUnknownDataType = Error("unknown data type")
// ErrIllegalParamName - a parameter has an illegal name // ErrIllegalParamName - a parameter has an illegal name
const ErrIllegalParamName = Error("parameter name must not begin/end with '_'") const ErrIllegalParamName = Error("parameter name must not begin/end with '_'")

15
config/func.go Normal file
View File

@ -0,0 +1,15 @@
package config
import "strings"
// splits an URL without empty sets
func splitURL(url string) []string {
trimmed := strings.Trim(url, " /\t\r\n")
split := strings.Split(trimmed, "/")
// remove empty set when empty url
if len(split) == 1 && len(split[0]) == 0 {
return []string{}
}
return split
}

View File

@ -1,5 +1,7 @@
package config package config
import "git.xdrm.io/go/aicra/config/datatype"
func (param *Parameter) checkAndFormat() error { func (param *Parameter) checkAndFormat() error {
// missing description // missing description
@ -20,3 +22,14 @@ func (param *Parameter) checkAndFormat() error {
return nil return nil
} }
// assigns the first matching data type from the type definition
func (param *Parameter) assignDataType(types []datatype.DataType) bool {
for _, dtype := range types {
param.Validator = dtype.Build(param.Type)
if param.Validator != nil {
return true
}
}
return false
}

View File

@ -4,11 +4,26 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"git.xdrm.io/go/aicra/config/datatype"
) )
// Match returns if this service would handle this HTTP request // Match returns if this service would handle this HTTP request
func (svc *Service) Match(req *http.Request) bool { func (svc *Service) Match(req *http.Request) bool {
return false // method
if req.Method != svc.Method {
return false
}
// check path
if !svc.matchPattern(req.RequestURI) {
return false
}
// check and extract input
// todo: check if input match
return true
} }
func (svc *Service) checkMethod() error { func (svc *Service) checkMethod() error {
@ -68,7 +83,7 @@ func (svc *Service) checkPattern() error {
return nil return nil
} }
func (svc *Service) checkAndFormatInput() error { func (svc *Service) checkAndFormatInput(types []datatype.DataType) error {
// ignore no parameter // ignore no parameter
if svc.Input == nil || len(svc.Input) < 1 { if svc.Input == nil || len(svc.Input) < 1 {
@ -94,6 +109,10 @@ func (svc *Service) checkAndFormatInput() error {
return fmt.Errorf("%s: %w", paramName, err) return fmt.Errorf("%s: %w", paramName, err)
} }
if !param.assignDataType(types) {
return fmt.Errorf("%s: %w", paramName, ErrUnknownDataType)
}
// check for name/rename conflict // check for name/rename conflict
for paramName2, param2 := range svc.Input { for paramName2, param2 := range svc.Input {
// ignore self // ignore self
@ -114,3 +133,48 @@ func (svc *Service) checkAndFormatInput() error {
return nil return nil
} }
// checks if an uri matches the service's pattern
func (svc *Service) matchPattern(uri string) bool {
uriparts := splitURL(uri)
parts := splitURL(svc.Pattern)
// fail if size differ
if len(uriparts) != len(parts) {
return false
}
// root url '/'
if len(parts) == 0 {
return true
}
// check part by part
for i, part := range parts {
uripart := uriparts[i]
isCapture := len(part) > 0 && part[0] == '{'
// if no capture -> check equality
if !isCapture {
if part != uripart {
return false
}
continue
}
param, exists := svc.Input[part]
// fail if no validator
if !exists || param.Validator == nil {
return false
}
// fail if not type-valid
if _, valid := param.Validator(uripart); !valid {
return false
}
}
return true
}

View File

@ -6,38 +6,123 @@ import (
"io" "io"
"net/http" "net/http"
"strings" "strings"
"git.xdrm.io/go/aicra/config/datatype"
) )
// Parse builds a server configuration from a json reader and checks for most format errors. // Parse builds a server configuration from a json reader and checks for most format errors.
func Parse(r io.Reader) (Services, error) { // you can provide additional DataTypes as variadic arguments
services := make(Services, 0) func Parse(r io.Reader, dtypes ...datatype.DataType) (*Server, error) {
server := &Server{
types: make([]datatype.DataType, 0),
services: make([]*Service, 0),
}
// add data types
for _, dtype := range dtypes {
server.types = append(server.types, dtype)
}
err := json.NewDecoder(r).Decode(&services) // parse JSON
if err != nil { if err := json.NewDecoder(r).Decode(&server.services); err != nil {
return nil, fmt.Errorf("%s: %w", ErrRead, err) return nil, fmt.Errorf("%s: %w", ErrRead, err)
} }
err = services.checkAndFormat() // check services
if err != nil { if err := server.checkAndFormat(); err != nil {
return nil, fmt.Errorf("%s: %w", ErrFormat, err) return nil, fmt.Errorf("%s: %w", ErrFormat, err)
} }
if services.collide() { // check collisions
return nil, fmt.Errorf("%s: %w", ErrFormat, ErrPatternCollision) if err := server.collide(); err != nil {
return nil, fmt.Errorf("%s: %w", ErrFormat, err)
} }
return services, nil return server, nil
} }
// collide returns if there is collision between services // collide returns if there is collision between services
func (svc Services) collide() bool { func (server *Server) collide() error {
// todo: implement pattern collision using types to check if braces can be equal to fixed uri parts length := len(server.services)
return false
// for each service combination
for a := 0; a < length; a++ {
for b := a + 1; b < length; b++ {
aService := server.services[a]
bService := server.services[b]
// ignore different method
if aService.Method != bService.Method {
continue
}
aParts := splitURL(aService.Pattern)
bParts := splitURL(bService.Pattern)
// not same size
if len(aParts) != len(bParts) {
continue
}
// for each part
for pi, aPart := range aParts {
bPart := bParts[pi]
aIsCapture := len(aPart) > 1 && aPart[0] == '{'
bIsCapture := len(bPart) > 1 && bPart[0] == '{'
// both captures -> as we cannot check, consider a collision
if aIsCapture && bIsCapture {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern)
}
// no capture -> check equal
if !aIsCapture && !bIsCapture {
if aPart == bPart {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern)
}
continue
}
// A captures B -> check type (B is A ?)
if aIsCapture {
input, exists := aService.Input[aPart]
// fail if no type or no validator
if !exists || input.Validator == nil {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern)
}
// fail if not valid
if _, valid := input.Validator(aPart); !valid {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern)
}
// B captures A -> check type (A is B ?)
} else {
input, exists := bService.Input[bPart]
// fail if no type or no validator
if !exists || input.Validator == nil {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern)
}
// fail if not valid
if _, valid := input.Validator(bPart); !valid {
return fmt.Errorf("%s: %s '%s'", ErrPatternCollision, aService.Method, aService.Pattern)
}
}
}
}
}
return nil
} }
// Find a service matching an incoming HTTP request // Find a service matching an incoming HTTP request
func (svc Services) Find(r *http.Request) *Service { func (server Server) Find(r *http.Request) *Service {
for _, service := range svc { for _, service := range server.services {
if service.Match(r) { if service.Match(r) {
return service return service
} }
@ -47,8 +132,8 @@ func (svc Services) Find(r *http.Request) *Service {
} }
// checkAndFormat checks for errors and missing fields and sets default values for optional fields. // checkAndFormat checks for errors and missing fields and sets default values for optional fields.
func (svc Services) checkAndFormat() error { func (server Server) checkAndFormat() error {
for _, service := range svc { for _, service := range server.services {
// check method // check method
err := service.checkMethod() err := service.checkMethod()
@ -69,7 +154,7 @@ func (svc Services) checkAndFormat() error {
} }
// check input parameters // check input parameters
err = service.checkAndFormatInput() err = service.checkAndFormatInput(server.types)
if err != nil { if err != nil {
return fmt.Errorf("%s '%s' [in]: %w", service.Method, service.Pattern, err) return fmt.Errorf("%s '%s' [in]: %w", service.Method, service.Pattern, err)
} }

View File

@ -13,10 +13,11 @@ type Parameter struct {
Description string `json:"info"` Description string `json:"info"`
Type string `json:"type"` Type string `json:"type"`
Rename string `json:"name,omitempty"` Rename string `json:"name,omitempty"`
Optional bool // Optional is set to true when the type is prefixed with '?'
Optional bool
// validator is set from the @Type // Validator is inferred from @Type
validator datatype.Validator Validator datatype.Validator
} }
// Service represents a service definition (from api.json) // Service represents a service definition (from api.json)
@ -25,10 +26,13 @@ type Service struct {
Pattern string `json:"path"` Pattern string `json:"path"`
Scope [][]string `json:"scope"` Scope [][]string `json:"scope"`
Description string `json:"info"` Description string `json:"info"`
Download *bool `json:"download"`
Input map[string]*Parameter `json:"in"` Input map[string]*Parameter `json:"in"`
// Download *bool `json:"download"`
// Output map[string]*Parameter `json:"out"` // Output map[string]*Parameter `json:"out"`
} }
// Services contains every service that represents a server configuration // Server represents a full server configuration
type Services []*Service type Server struct {
types []datatype.DataType
services []*Service
}