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"
)
// 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

View File

@ -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]

View File

@ -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 {

View File

@ -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"))
}
}

View File

@ -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 {