♻️ Use defined errors
This commit is contained in:
parent
5b60305de7
commit
2d68691408
5 changed files with 34 additions and 20 deletions
|
|
@ -29,12 +29,12 @@ func (e Entry) getFieldMarshalChan() chan metaRes {
|
||||||
// @todo figure out a way to handle json field
|
// @todo figure out a way to handle json field
|
||||||
for i := 0; i < size; i++ {
|
for i := 0; i < size; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(i int) {
|
go func(m Meta) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
o, er := e.Fields[i].MarshalText()
|
o, er := m.MarshalText()
|
||||||
ch <- metaRes{o, er}
|
ch <- metaRes{o, er}
|
||||||
|
|
||||||
}(i)
|
}(e.Fields[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -47,10 +47,10 @@ func (e Entry) getFieldMarshalChan() chan metaRes {
|
||||||
func (e Entry) MarshalText() ([]byte, error) {
|
func (e Entry) MarshalText() ([]byte, error) {
|
||||||
e.Title = strings.TrimSpace(e.Title)
|
e.Title = strings.TrimSpace(e.Title)
|
||||||
if e.Title == "" {
|
if e.Title == "" {
|
||||||
return []byte{}, errors.New("Empty title")
|
return []byte{}, ErrorMissingTitle
|
||||||
}
|
}
|
||||||
if e.Date == (time.Time{}) {
|
if e.Date == (time.Time{}) {
|
||||||
return []byte{}, errors.New("Empty date")
|
return []byte{}, ErrorMissingDate
|
||||||
}
|
}
|
||||||
ch := e.getFieldMarshalChan()
|
ch := e.getFieldMarshalChan()
|
||||||
buff := &bytes.Buffer{}
|
buff := &bytes.Buffer{}
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,8 @@ func TestEntryMarshal(t *testing.T) {
|
||||||
lines []string
|
lines []string
|
||||||
err error
|
err error
|
||||||
}{
|
}{
|
||||||
{"no-title", "", when, simple, "", nolines, errors.New("Empty title")},
|
{"no-title", "", when, simple, "", nolines, ErrorMissingTitle},
|
||||||
{"zero-date", "Empty title", time.Time{}, simple, "", nolines, errors.New("Empty date")},
|
{"zero-date", "Empty title", time.Time{}, simple, "", nolines, ErrorMissingDate},
|
||||||
{"one-line", "A Title", when, simple, "@begin " + whens + " - A Title @end", nolines, nil},
|
{"one-line", "A Title", when, simple, "@begin " + whens + " - A Title @end", nolines, nil},
|
||||||
{
|
{
|
||||||
"one-field",
|
"one-field",
|
||||||
|
|
@ -102,8 +102,8 @@ func TestEntryUnmarshal(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{"one-line", "@begin " + whens + " - A Title @end", "A Title", when, simple, nil},
|
{"one-line", "@begin " + whens + " - A Title @end", "A Title", when, simple, nil},
|
||||||
{"multi-title", "@begin " + whens + " - A Title\nwith break @end", "A Title\nwith break", when, simple, nil},
|
{"multi-title", "@begin " + whens + " - A Title\nwith break @end", "A Title\nwith break", when, simple, nil},
|
||||||
{"no-title", "@begin " + whens + " - @end", "", when, simple, errors.New("Missing title")},
|
{"no-title", "@begin " + whens + " - @end", "", when, simple, errors.New("Missing title")},
|
||||||
{"no-date", "@begin - A Title @end", "A Title", when, simple, errors.New("Missing date")},
|
{"no-date", "@begin - A Title @end", "A Title", when, simple, errors.New("Missing date")},
|
||||||
{"one-field", "@begin " + whens + " - A Title\n@age 41 @end", "A Title", when, []Meta{{"age", 41}}, nil},
|
{"one-field", "@begin " + whens + " - A Title\n@age 41 @end", "A Title", when, []Meta{{"age", 41}}, nil},
|
||||||
{
|
{
|
||||||
"two-fields",
|
"two-fields",
|
||||||
|
|
|
||||||
14
models/errors.go
Normal file
14
models/errors.go
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrorMissingTitle = errors.New("Missing title")
|
||||||
|
var ErrorMissingDate = errors.New("Missing date")
|
||||||
|
var ErrorParsing = errors.New("Parsing Error")
|
||||||
|
|
||||||
|
func newParsingError(err error) error {
|
||||||
|
return fmt.Errorf("%w: %w", ErrorParsing, err)
|
||||||
|
}
|
||||||
|
|
@ -56,12 +56,12 @@ func (m Meta) MarshalText() ([]byte, error) {
|
||||||
|
|
||||||
func (m *Meta) UnmarshalText(in []byte) error {
|
func (m *Meta) UnmarshalText(in []byte) error {
|
||||||
if len(in) == 0 {
|
if len(in) == 0 {
|
||||||
return errors.New("Unable to Unmarshal empty string")
|
return newParsingError(errors.New("Unable to Unmarshal empty string"))
|
||||||
}
|
}
|
||||||
re := regexp.MustCompile("(?s)^@([^ ]+) (.*)( @end)?$")
|
re := regexp.MustCompile("(?s)^@([^ ]+) (.*)( @end)?$")
|
||||||
match := re.FindSubmatch(in)
|
match := re.FindSubmatch(in)
|
||||||
if len(match) == 0 {
|
if len(match) == 0 {
|
||||||
return fmt.Errorf("Failed to match %s", in)
|
return newParsingError(fmt.Errorf("Failed to match %s", in))
|
||||||
}
|
}
|
||||||
m.Key = string(match[1])
|
m.Key = string(match[1])
|
||||||
return m.processMeta(match[2])
|
return m.processMeta(match[2])
|
||||||
|
|
@ -69,11 +69,11 @@ func (m *Meta) UnmarshalText(in []byte) error {
|
||||||
|
|
||||||
func (m *Meta) processMeta(in []byte) error {
|
func (m *Meta) processMeta(in []byte) error {
|
||||||
if len(in) == 0 {
|
if len(in) == 0 {
|
||||||
return errors.New("No value found")
|
return newParsingError(errors.New("No value found"))
|
||||||
}
|
}
|
||||||
s := strings.TrimSpace(string(in))
|
s := strings.TrimSpace(string(in))
|
||||||
if len(s) == 0 {
|
if len(s) == 0 {
|
||||||
return errors.New("No value found")
|
return newParsingError(errors.New("No value found"))
|
||||||
}
|
}
|
||||||
yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$")
|
yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$")
|
||||||
yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$")
|
yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$")
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ func TestMeta(t *testing.T) {
|
||||||
{"json number", "num", json.Number("42.13"), "@num 42.13", nil, 42.13},
|
{"json number", "num", json.Number("42.13"), "@num 42.13", nil, 42.13},
|
||||||
{"true", "b", true, "@b true", nil, true},
|
{"true", "b", true, "@b true", nil, true},
|
||||||
{"false", "b", false, "@b false", nil, false},
|
{"false", "b", false, "@b false", nil, false},
|
||||||
{"nil", "n", nil, "", nil, errors.New("Unable to Unmarshal empty string")},
|
{"nil", "n", nil, "", nil, ErrorParsing},
|
||||||
{"time", "when", when, "@when " + when.Format(time.RFC3339), nil, when},
|
{"time", "when", when, "@when " + when.Format(time.RFC3339), nil, when},
|
||||||
{"rune", "char", '@', "@char @", nil, "@"},
|
{"rune", "char", '@', "@char @", nil, "@"},
|
||||||
{"bytes", "byteme", []byte("yo"), "@byteme yo", nil, "yo"},
|
{"bytes", "byteme", []byte("yo"), "@byteme yo", nil, "yo"},
|
||||||
|
|
@ -41,11 +41,11 @@ func TestMeta(t *testing.T) {
|
||||||
{"json-arr", "arr", json.RawMessage(`["foo",42,"bar", null,"quux", true]`), `@arr ["foo",42,"bar", null,"quux", true]`, nil, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)},
|
{"json-arr", "arr", json.RawMessage(`["foo",42,"bar", null,"quux", true]`), `@arr ["foo",42,"bar", null,"quux", true]`, nil, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)},
|
||||||
{"chan", "nope", make(chan bool), "", errors.New("Unknown type chan bool"), ""},
|
{"chan", "nope", make(chan bool), "", errors.New("Unknown type chan bool"), ""},
|
||||||
{"whitespace-key", "no space", "hi", "", errors.New("whitespace is not allowed in key: no space"), ""},
|
{"whitespace-key", "no space", "hi", "", errors.New("whitespace is not allowed in key: no space"), ""},
|
||||||
{"empty-mar", "nope", skipMarshalTest, "", nil, errors.New("Unable to Unmarshal empty string")},
|
{"empty-mar", "nope", skipMarshalTest, "", nil, ErrorParsing},
|
||||||
{"no-key-mar", "nope", skipMarshalTest, "nope", nil, errors.New("Failed to match nope")},
|
{"no-key-mar", "nope", skipMarshalTest, "nope", nil, ErrorParsing},
|
||||||
{"no-value-mar", "nope", skipMarshalTest, "@nope ", nil, errors.New("No value found")},
|
{"no-value-mar", "nope", skipMarshalTest, "@nope ", nil, ErrorParsing},
|
||||||
{"space-value-mar", "nope", skipMarshalTest, "@nope ", nil, errors.New("No value found")},
|
{"space-value-mar", "nope", skipMarshalTest, "@nope ", nil, ErrorParsing},
|
||||||
{"space-value-mar", "nope", skipMarshalTest, "@nope \n ", nil, errors.New("No value found")},
|
{"space-nl-value-mar", "nope", skipMarshalTest, "@nope \n ", nil, ErrorParsing},
|
||||||
{"null-value-mar", "nope", skipMarshalTest, "@nope null", nil, nil},
|
{"null-value-mar", "nope", skipMarshalTest, "@nope null", nil, nil},
|
||||||
{"tilda-value-mar", "nope", skipMarshalTest, "@nope ~", nil, nil},
|
{"tilda-value-mar", "nope", skipMarshalTest, "@nope ~", nil, nil},
|
||||||
{"none-value-mar", "nope", skipMarshalTest, "@nope none", nil, nil},
|
{"none-value-mar", "nope", skipMarshalTest, "@nope none", nil, nil},
|
||||||
|
|
@ -81,7 +81,7 @@ func getMetaTestRunner(key string, value any, out string, err error, newVal any)
|
||||||
e = n.UnmarshalText([]byte(out))
|
e = n.UnmarshalText([]byte(out))
|
||||||
}
|
}
|
||||||
if newE, ok := newVal.(error); ok {
|
if newE, ok := newVal.(error); ok {
|
||||||
assert.Equal(t, newE, e)
|
assert.ErrorIs(t, e, newE)
|
||||||
} else {
|
} else {
|
||||||
assert.Equal(t, key, n.Key)
|
assert.Equal(t, key, n.Key)
|
||||||
if ti, ok := newVal.(time.Time); ok {
|
if ti, ok := newVal.(time.Time); ok {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue