ref: clean comments and make errors constants

This commit is contained in:
Adrien Marquès 2019-05-04 10:04:16 +02:00
parent 0780e8dd33
commit bf3e651c2c
5 changed files with 59 additions and 57 deletions

View File

@ -10,30 +10,27 @@ import (
"git.xdrm.io/go/aicra/internal/cerr" "git.xdrm.io/go/aicra/internal/cerr"
) )
// ErrReadConfig - a problem ocurred when trying to read the configuration file // ErrRead - a problem ocurred when trying to read the configuration file
const ErrReadConfig = cerr.Error("cannot read config") const ErrRead = cerr.Error("cannot read config")
// ErrFormatConfig - a invalid format has been detected // ErrFormat - a invalid format has been detected
const ErrFormatConfig = cerr.Error("invalid config format") const ErrFormat = cerr.Error("invalid config format")
// Parse builds a service from a json reader and checks for most format errors. // Parse builds a service from a json reader and checks for most format errors.
func Parse(r io.Reader) (*Service, error) { func Parse(r io.Reader) (*Service, error) {
receiver := &Service{} receiver := &Service{}
err := json.NewDecoder(r).Decode(receiver) err := json.NewDecoder(r).Decode(receiver)
if err != nil { if err != nil {
return nil, ErrReadConfig.Wrap(err) return nil, ErrRead.Wrap(err)
} }
ErrFormatConfig.Wrap(fmt.Errorf("blable"))
err = receiver.checkAndFormat("/") err = receiver.checkAndFormat("/")
if err != nil { if err != nil {
return nil, ErrFormatConfig.Wrap(err) return nil, ErrFormat.Wrap(err)
} }
return receiver, nil return receiver, nil
} }
// Method returns the actual method from the http method. // Method returns the actual method from the http method.
@ -54,7 +51,7 @@ func (svc *Service) Method(httpMethod string) *Method {
return nil 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) { func (svc *Service) Browse(path []string) (*Service, int) {
currentService := svc currentService := svc
var depth int var depth int

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
) )
// parseHeaders parses a component headers.
func (comp *Component) parseHeaders(_raw []byte) error { func (comp *Component) parseHeaders(_raw []byte) error {
// 1. Extract lines // 1. Extract lines
@ -59,10 +60,9 @@ func (comp *Component) parseHeaders(_raw []byte) error {
} }
return nil 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 { func (comp *Component) GetHeader(_key string) string {
value, ok := comp.Headers[_key] value, ok := comp.Headers[_key]

View File

@ -8,17 +8,17 @@ import (
// Reader is a multipart reader. // Reader is a multipart reader.
type Reader struct { type Reader struct {
// io.Reader used for http.Request.Body reading // io.Reader used for reading multipart components reading.
reader *bufio.Reader reader *bufio.Reader
// boundary used to separate multipart MultipartDatas // boundary used to separate multipart components.
boundary string boundary string
// result will be inside this field // data will contain parsed components.
Data map[string]*Component 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) { func NewReader(r io.Reader, boundary string) (*Reader, error) {
reader := &Reader{ reader := &Reader{
reader: nil, 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 { func (reader *Reader) Parse() error {
// for each component (until boundary) // 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 { func (reader *Reader) Get(_key string) *Component {
data, ok := reader.Data[_key] data, ok := reader.Data[_key]
if !ok { if !ok {

View File

@ -24,47 +24,49 @@ facebook.com
} }
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil { if err != nil {
t.Fatalf("Unexpected error <%s>", err) t.Fatalf("unexpected error: %s", err)
} }
if err = mpr.Parse(); err != nil { if err = mpr.Parse(); err != nil {
t.Fatalf("Unexpected error <%s>", err) t.Fatalf("unexpected error: %s", err)
} }
// 1. Check var // 1. Check var
somevar := mpr.Get("somevar") somevar := mpr.Get("somevar")
if somevar == nil { if somevar == nil {
t.Fatalf("Expected data {%s} to exist", "somevar") t.Fatalf("expected data %q to exist", "somevar")
} }
if somevar.ContentType != "raw" { 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" { 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 // 2. Check file
somefile := mpr.Get("somefile") somefile := mpr.Get("somefile")
if somefile == nil { if somefile == nil {
t.Fatalf("Expected data {%s} to exist", "somefile") t.Fatalf("expected data %q to exist", "somefile")
} }
if somefile.ContentType != "application/pdf" { const fileCT = "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" { const fileData = "facebook.com"
t.Fatalf("Expected data to be {%s}, got {%s}", "facebook.com", somefile.Data) 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") filename := somefile.GetHeader("filename")
if len(filename) < 1 { if len(filename) < 1 {
t.Fatalf("Expected data to have header 'filename'") t.Fatalf("expected data to have header 'filename'")
} }
if filename != "somefilename.pdf" { if filename != fileHeader {
t.Fatalf("Expected filename to be {%s}, got {%s}", "somefilename.pdf", filename) 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) mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil { if err != nil {
t.Fatalf("Unexpected error <%s>", err) t.Fatalf("unexpected error <%s>", err)
} }
if err = mpr.Parse(); err != nil { if err = mpr.Parse(); err != nil {
t.Fatalf("Unexpected error <%s>", err) t.Fatalf("unexpected error <%s>", err)
} }
// 1. Check var // 1. Check var
somevar := mpr.Get("somevar") somevar := mpr.Get("somevar")
if somevar == nil { if somevar == nil {
t.Fatalf("Expected data {%s} to exist", "somevar") t.Fatalf("expected data %q to exist", "somevar")
} }
if somevar.ContentType != "raw" { 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" { 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 // 2. Check file
const fileCT = "application/pdf"
somefile := mpr.Get("somefile") somefile := mpr.Get("somefile")
if somefile == nil { if somefile == nil {
t.Fatalf("Expected data {%s} to exist", "somefile") t.Fatalf("expected data %q to exist", "somefile")
} }
if somefile.ContentType != "application/pdf" { if somefile.ContentType != fileCT {
t.Fatalf("Expected ContentType to be {application/pdf}, got {%s}", somevar.ContentType) t.Fatalf("expected ContentType to be %q, got %q", fileCT, somevar.ContentType)
} }
if string(somefile.Data) != "facebook.com" { const fileData = "facebook.com"
t.Fatalf("Expected data to be {%s}, got {%s}", "facebook.com", somefile.Data) 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") filename := somefile.GetHeader("filename")
if len(filename) < 1 { if len(filename) < 1 {
t.Fatalf("Expected data to have header 'filename'") t.Fatalf("expected data to have header 'filename'")
} }
if filename != "somefilename.pdf" { if filename != fileHeader {
t.Fatalf("Expected filename to be {%s}, got {%s}", "somefilename.pdf", filename) 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) mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil { if err != nil {
t.Errorf("(%d) Unexpected error <%s>", i, err) t.Errorf("%d: unexpected error <%s>", i, err)
continue continue
} }
if err = mpr.Parse(); err != ErrMissingDataName { 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 continue
} }
@ -217,12 +222,12 @@ func TestNoHeader(t *testing.T) {
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil { if err != nil {
t.Errorf("(%d) Unexpected error <%s>", i, err) t.Errorf("%d: unexpected error <%s>", i, err)
continue continue
} }
if err = mpr.Parse(); err != ErrNoHeader { 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 continue
} }
@ -251,11 +256,11 @@ facebook.com
mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary) mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil { if err != nil {
t.Fatalf("Unexpected error <%s>", err) t.Fatalf("unexpected error <%s>", err)
} }
if err = mpr.Parse(); err != ErrDataNameConflict { 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) mpr, err := NewReader(bytes.NewReader(test.Input), test.Boundary)
if err != nil { if err != nil {
t.Fatalf("Unexpected error <%s>", err) t.Fatalf("unexpected error <%s>", err)
} }
if err = mpr.Parse(); err != nil { if err = mpr.Parse(); err != nil {
t.Fatalf("Unexpected error <%s>", err) t.Fatalf("unexpected error <%s>", err)
} }
if mpr.Get("unknown_key") != nil { 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"))
} }
} }

View File

@ -9,13 +9,13 @@ func (err ConstError) Error() string {
} }
// ErrMissingDataName is set when a multipart variable/file has no name="..." // 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 // 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 // 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 // Component represents a multipart variable/file
type Component struct { type Component struct {