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

View File

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