make config.Service members public

This commit is contained in:
Adrien Marquès 2020-03-16 09:01:51 +01:00
parent 2c1b9cf5ff
commit 4e0d669029
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
3 changed files with 19 additions and 19 deletions

View File

@ -233,12 +233,12 @@ func TestParamEmptyRenameNoRename(t *testing.T) {
t.FailNow() t.FailNow()
} }
if len(srv.services) < 1 { if len(srv.Services) < 1 {
t.Errorf("expected a service") t.Errorf("expected a service")
t.FailNow() t.FailNow()
} }
for _, param := range srv.services[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()
@ -266,11 +266,11 @@ func TestOptionalParam(t *testing.T) {
t.FailNow() t.FailNow()
} }
if len(srv.services) < 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.services[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 {
@ -618,14 +618,14 @@ func TestMatchSimple(t *testing.T) {
t.FailNow() t.FailNow()
} }
if len(srv.services) != 1 { if len(srv.Services) != 1 {
t.Errorf("expected to have 1 service, got %d", len(srv.services)) t.Errorf("expected to have 1 service, got %d", len(srv.Services))
t.FailNow() t.FailNow()
} }
req := httptest.NewRequest(http.MethodGet, test.URL, nil) req := httptest.NewRequest(http.MethodGet, test.URL, nil)
match := srv.services[0].Match(req) match := srv.Services[0].Match(req)
if test.Match && !match { if test.Match && !match {
t.Errorf("expected '%s' to match", test.URL) t.Errorf("expected '%s' to match", test.URL)
t.FailNow() t.FailNow()

View File

@ -14,16 +14,16 @@ import (
// you can provide additional DataTypes as variadic arguments // you can provide additional DataTypes as variadic arguments
func Parse(r io.Reader, dtypes ...datatype.DataType) (*Server, error) { func Parse(r io.Reader, dtypes ...datatype.DataType) (*Server, error) {
server := &Server{ server := &Server{
types: make([]datatype.DataType, 0), Types: make([]datatype.DataType, 0),
services: make([]*Service, 0), Services: make([]*Service, 0),
} }
// add data types // add data types
for _, dtype := range dtypes { for _, dtype := range dtypes {
server.types = append(server.types, dtype) server.Types = append(server.Types, dtype)
} }
// parse JSON // parse JSON
if err := json.NewDecoder(r).Decode(&server.services); 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)
} }
@ -42,13 +42,13 @@ func Parse(r io.Reader, dtypes ...datatype.DataType) (*Server, error) {
// collide returns if there is collision between services // collide returns if there is collision between services
func (server *Server) collide() error { func (server *Server) collide() error {
length := len(server.services) length := len(server.Services)
// for each service combination // for each service combination
for a := 0; a < length; a++ { for a := 0; a < length; a++ {
for b := a + 1; b < length; b++ { for b := a + 1; b < length; b++ {
aService := server.services[a] aService := server.Services[a]
bService := server.services[b] bService := server.Services[b]
// ignore different method // ignore different method
if aService.Method != bService.Method { if aService.Method != bService.Method {
@ -122,7 +122,7 @@ func (server *Server) collide() error {
// Find a service matching an incoming HTTP request // Find a service matching an incoming HTTP request
func (server Server) Find(r *http.Request) *Service { func (server Server) Find(r *http.Request) *Service {
for _, service := range server.services { for _, service := range server.Services {
if matches := service.Match(r); matches { if matches := service.Match(r); matches {
return service return service
} }
@ -133,7 +133,7 @@ func (server Server) 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 (server Server) checkAndFormat() error { func (server Server) checkAndFormat() error {
for _, service := range server.services { for _, service := range server.Services {
// check method // check method
err := service.checkMethod() err := service.checkMethod()
@ -154,7 +154,7 @@ func (server Server) checkAndFormat() error {
} }
// check input parameters // check input parameters
err = service.checkAndFormatInput(server.types) 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

@ -10,8 +10,8 @@ var availableHTTPMethods = []string{http.MethodGet, http.MethodPost, http.Method
// Server represents a full server configuration // Server represents a full server configuration
type Server struct { type Server struct {
types []datatype.DataType Types []datatype.DataType
services []*Service Services []*Service
} }
// Service represents a service definition (from api.json) // Service represents a service definition (from api.json)