From bf3e651c2ca893d24a28bcb618bf9742eb4d8671 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 4 May 2019 10:04:16 +0200 Subject: [PATCH] ref: clean comments and make errors constants --- internal/config/service.go | 17 +++---- internal/multipart/component.go | 4 +- internal/multipart/reader.go | 12 ++--- internal/multipart/reader_test.go | 77 ++++++++++++++++--------------- internal/multipart/types.go | 6 +-- 5 files changed, 59 insertions(+), 57 deletions(-) diff --git a/internal/config/service.go b/internal/config/service.go index 0b46b18..cf223fe 100644 --- a/internal/config/service.go +++ b/internal/config/service.go @@ -10,30 +10,27 @@ import ( "git.xdrm.io/go/aicra/internal/cerr" ) -// ErrReadConfig - a problem ocurred when trying to read the configuration file -const ErrReadConfig = cerr.Error("cannot read config") +// ErrRead - a problem ocurred when trying to read the configuration file +const ErrRead = cerr.Error("cannot read config") -// ErrFormatConfig - a invalid format has been detected -const ErrFormatConfig = cerr.Error("invalid config format") +// ErrFormat - a invalid format has been detected +const ErrFormat = cerr.Error("invalid config format") // Parse builds a service from a json reader and checks for most format errors. func Parse(r io.Reader) (*Service, error) { - receiver := &Service{} err := json.NewDecoder(r).Decode(receiver) if err != nil { - return nil, ErrReadConfig.Wrap(err) + return nil, ErrRead.Wrap(err) } - ErrFormatConfig.Wrap(fmt.Errorf("blable")) err = receiver.checkAndFormat("/") if err != nil { - return nil, ErrFormatConfig.Wrap(err) + return nil, ErrFormat.Wrap(err) } return receiver, nil - } // Method returns the actual method from the http method. @@ -54,7 +51,7 @@ func (svc *Service) Method(httpMethod string) *Method { return nil } -// Browse the service childtree and returns the farthest matching child. The `path` is a formatted URL split by '/' +// Browse the service childtree and returns the deepest matching child. The `path` is a formatted URL split by '/' func (svc *Service) Browse(path []string) (*Service, int) { currentService := svc var depth int diff --git a/internal/multipart/component.go b/internal/multipart/component.go index 5f74ef4..df4d8bf 100644 --- a/internal/multipart/component.go +++ b/internal/multipart/component.go @@ -7,6 +7,7 @@ import ( "strings" ) +// parseHeaders parses a component headers. func (comp *Component) parseHeaders(_raw []byte) error { // 1. Extract lines @@ -59,10 +60,9 @@ func (comp *Component) parseHeaders(_raw []byte) error { } return nil - } -// GetHeader returns the header value associated with a key, empty string if not found +// GetHeader returns the header value associated with a key. func (comp *Component) GetHeader(_key string) string { value, ok := comp.Headers[_key] diff --git a/internal/multipart/reader.go b/internal/multipart/reader.go index dda82e4..affb80a 100644 --- a/internal/multipart/reader.go +++ b/internal/multipart/reader.go @@ -8,17 +8,17 @@ import ( // Reader is a multipart reader. type Reader struct { - // io.Reader used for http.Request.Body reading + // io.Reader used for reading multipart components reading. reader *bufio.Reader - // boundary used to separate multipart MultipartDatas + // boundary used to separate multipart components. boundary string - // result will be inside this field + // data will contain parsed components. Data map[string]*Component } -// NewReader creates a new reader from a reader and a boundary. +// NewReader creates a new multipart reader for a reader and a boundary. func NewReader(r io.Reader, boundary string) (*Reader, error) { reader := &Reader{ reader: nil, @@ -49,7 +49,7 @@ func NewReader(r io.Reader, boundary string) (*Reader, error) { } -// Parse parses the multipart components from the request +// Parse parses the multipart components from the reader. func (reader *Reader) Parse() error { // for each component (until boundary) @@ -88,7 +88,7 @@ func (reader *Reader) Parse() error { } -// Get returns a multipart data by name, nil if not found +// Get returns a multipart component by its name. func (reader *Reader) Get(_key string) *Component { data, ok := reader.Data[_key] if !ok { diff --git a/internal/multipart/reader_test.go b/internal/multipart/reader_test.go index f1c385e..03a0d7a 100644 --- a/internal/multipart/reader_test.go +++ b/internal/multipart/reader_test.go @@ -24,47 +24,49 @@ facebook.com } mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) - if err != nil { - t.Fatalf("Unexpected error <%s>", err) + t.Fatalf("unexpected error: %s", err) } if err = mpr.Parse(); err != nil { - t.Fatalf("Unexpected error <%s>", err) + t.Fatalf("unexpected error: %s", err) } // 1. Check var somevar := mpr.Get("somevar") if somevar == nil { - t.Fatalf("Expected data {%s} to exist", "somevar") + t.Fatalf("expected data %q to exist", "somevar") } if somevar.ContentType != "raw" { - t.Fatalf("Expected ContentType to be {raw}, got {%s}", somevar.ContentType) + t.Fatalf("expected ContentType to be %q, got %q", "raw", somevar.ContentType) } if string(somevar.Data) != "google.com" { - t.Fatalf("Expected data to be {%s}, got {%s}", "google.com", somevar.Data) + t.Fatalf("expected data to be %q, got %q", "google.com", somevar.Data) } // 2. Check file somefile := mpr.Get("somefile") if somefile == nil { - t.Fatalf("Expected data {%s} to exist", "somefile") + t.Fatalf("expected data %q to exist", "somefile") } - if somefile.ContentType != "application/pdf" { - t.Fatalf("Expected ContentType to be {application/pdf}, got {%s}", somevar.ContentType) + const fileCT = "application/pdf" + if somefile.ContentType != fileCT { + t.Fatalf("expected ContentType to be %q, got %q", fileCT, somevar.ContentType) } - if string(somefile.Data) != "facebook.com" { - t.Fatalf("Expected data to be {%s}, got {%s}", "facebook.com", somefile.Data) + const fileData = "facebook.com" + if string(somefile.Data) != fileData { + t.Fatalf("expected data to be %q, got %q", fileData, somefile.Data) } + const fileHeader = "somefilename.pdf" filename := somefile.GetHeader("filename") if len(filename) < 1 { - t.Fatalf("Expected data to have header 'filename'") + t.Fatalf("expected data to have header 'filename'") } - if filename != "somefilename.pdf" { - t.Fatalf("Expected filename to be {%s}, got {%s}", "somefilename.pdf", filename) + if filename != fileHeader { + t.Fatalf("expected filename to be %q, got %q", fileHeader, filename) } } @@ -107,45 +109,48 @@ facebook.com mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) if err != nil { - t.Fatalf("Unexpected error <%s>", err) + t.Fatalf("unexpected error <%s>", err) } if err = mpr.Parse(); err != nil { - t.Fatalf("Unexpected error <%s>", err) + t.Fatalf("unexpected error <%s>", err) } // 1. Check var somevar := mpr.Get("somevar") if somevar == nil { - t.Fatalf("Expected data {%s} to exist", "somevar") + t.Fatalf("expected data %q to exist", "somevar") } if somevar.ContentType != "raw" { - t.Fatalf("Expected ContentType to be {raw}, got {%s}", somevar.ContentType) + t.Fatalf("expected ContentType to be %q, got %q", "raw", somevar.ContentType) } if string(somevar.Data) != "google.com" { - t.Fatalf("Expected data to be {%s}, got {%s}", "google.com", somevar.Data) + t.Fatalf("expected data to be %q, got %q", "google.com", somevar.Data) } // 2. Check file + const fileCT = "application/pdf" somefile := mpr.Get("somefile") if somefile == nil { - t.Fatalf("Expected data {%s} to exist", "somefile") + t.Fatalf("expected data %q to exist", "somefile") } - if somefile.ContentType != "application/pdf" { - t.Fatalf("Expected ContentType to be {application/pdf}, got {%s}", somevar.ContentType) + if somefile.ContentType != fileCT { + t.Fatalf("expected ContentType to be %q, got %q", fileCT, somevar.ContentType) } - if string(somefile.Data) != "facebook.com" { - t.Fatalf("Expected data to be {%s}, got {%s}", "facebook.com", somefile.Data) + const fileData = "facebook.com" + if string(somefile.Data) != fileData { + t.Fatalf("expected data to be %q, got %q", fileData, somefile.Data) } + const fileHeader = "somefilename.pdf" filename := somefile.GetHeader("filename") if len(filename) < 1 { - t.Fatalf("Expected data to have header 'filename'") + t.Fatalf("expected data to have header 'filename'") } - if filename != "somefilename.pdf" { - t.Fatalf("Expected filename to be {%s}, got {%s}", "somefilename.pdf", filename) + if filename != fileHeader { + t.Fatalf("expected filename to be %q, got %q", fileHeader, filename) } } @@ -179,12 +184,12 @@ func TestNoName(t *testing.T) { mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) if err != nil { - t.Errorf("(%d) Unexpected error <%s>", i, err) + t.Errorf("%d: unexpected error <%s>", i, err) continue } if err = mpr.Parse(); err != ErrMissingDataName { - t.Errorf("(%d) Expected the error <%s>, got <%s>", i, ErrMissingDataName, err) + t.Errorf("%d: expected the error <%s>, got <%s>", i, ErrMissingDataName, err) continue } @@ -217,12 +222,12 @@ func TestNoHeader(t *testing.T) { mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) if err != nil { - t.Errorf("(%d) Unexpected error <%s>", i, err) + t.Errorf("%d: unexpected error <%s>", i, err) continue } if err = mpr.Parse(); err != ErrNoHeader { - t.Errorf("(%d) Expected the error <%s>, got <%s>", i, ErrNoHeader, err) + t.Errorf("%d: expected the error <%s>, got <%s>", i, ErrNoHeader, err) continue } @@ -251,11 +256,11 @@ facebook.com mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) if err != nil { - t.Fatalf("Unexpected error <%s>", err) + t.Fatalf("unexpected error <%s>", err) } if err = mpr.Parse(); err != ErrDataNameConflict { - t.Fatalf("Expected the error <%s>, got <%s>", ErrDataNameConflict, err) + t.Fatalf("expected the error <%s>, got <%s>", ErrDataNameConflict, err) } } @@ -281,15 +286,15 @@ facebook.com mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) if err != nil { - t.Fatalf("Unexpected error <%s>", err) + t.Fatalf("unexpected error <%s>", err) } if err = mpr.Parse(); err != nil { - t.Fatalf("Unexpected error <%s>", err) + t.Fatalf("unexpected error <%s>", err) } if mpr.Get("unknown_key") != nil { - t.Fatalf("Expected 'unknown_key' not to exist, got {%v}", mpr.Get("unknown_key")) + t.Fatalf("expected 'unknown_key' not to exist, got <%v>", mpr.Get("unknown_key")) } } diff --git a/internal/multipart/types.go b/internal/multipart/types.go index 01ed8c4..6860af4 100644 --- a/internal/multipart/types.go +++ b/internal/multipart/types.go @@ -9,13 +9,13 @@ func (err ConstError) Error() string { } // ErrMissingDataName is set when a multipart variable/file has no name="..." -var ErrMissingDataName = ConstError("data has no name") +const ErrMissingDataName = ConstError("data has no name") // ErrDataNameConflict is set when a multipart variable/file name is already used -var ErrDataNameConflict = ConstError("data name conflict") +const ErrDataNameConflict = ConstError("data name conflict") // ErrNoHeader is set when a multipart variable/file has no (valid) header -var ErrNoHeader = ConstError("data has no header") +const ErrNoHeader = ConstError("data has no header") // Component represents a multipart variable/file type Component struct {