Skip to content

Commit

Permalink
refactor repository
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed Nov 27, 2022
1 parent d025a4b commit 329a0b1
Show file tree
Hide file tree
Showing 19 changed files with 473 additions and 407 deletions.
4 changes: 4 additions & 0 deletions flint/builtins/builtins.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package builtins

import "github.com/hay-kot/flint/pkgs/frontmatter"

type CheckerFunc func(fm frontmatter.FrontMatter) error

type BuiltIns struct {
ID string
Level string
Expand Down
36 changes: 22 additions & 14 deletions flint/builtins/dateformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ package builtins

import (
"fmt"
"strings"
"time"

"github.com/hay-kot/flint/pkgs/frontmatter"
)

func (b BuiltIns) DateFormat(fm frontmatter.FrontMatter, format []string, fields []string) error {
errGroup := ErrGroup{
func (b BuiltIns) DateFormatFunc(formats []string, fields []string) CheckerFunc {
return func(fm frontmatter.FrontMatter) error {
return b.DateFormat(fm, formats, fields)
}
}

func (b BuiltIns) DateFormat(fm frontmatter.FrontMatter, formats []string, fields []string) error {
errGroup := ValueErrors{
ID: b.ID,
Level: b.Level,
Description: b.Description,
}

data := fm.Data()

outer:
for _, field := range fields {
value, ok := extractValue(data, strings.Split(field, "."))
value, ok := fm.Get(field)
if !ok {
continue
}
Expand All @@ -29,19 +31,25 @@ outer:
continue
}

for _, f := range format {
match := false

inner:
for _, f := range formats {
_, err := time.Parse(f, str)

if err == nil {
break outer
match = true
break inner
}
}

errGroup.Errors = append(errGroup.Errors, ErrGroupValue{
Line: fmtKeyCords(fm.KeyCords(field)),
Description: fmt.Sprintf("%q is not allowed format", str),
Field: field,
})
if !match {
errGroup.Errors = append(errGroup.Errors, ValueError{
Line: fmtKeyCords(fm.KeyCords(field)),
Description: fmt.Sprintf("%q is not allowed format", str),
Field: field,
})
}
}

if len(errGroup.Errors) > 0 {
Expand Down
35 changes: 23 additions & 12 deletions flint/builtins/dateformat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"testing"

"github.com/hay-kot/flint/pkgs/frontmatter"
"github.com/stretchr/testify/assert"
)

func TestBuiltIns_DateFormat(t *testing.T) {

type args struct {
format []string
fields []string
formats []string
fields []string
}
tests := []struct {
name string
Expand All @@ -21,36 +21,47 @@ func TestBuiltIns_DateFormat(t *testing.T) {
{
name: "simple case",
args: args{
format: []string{"2006-01-02"},
fields: []string{"date"},
formats: []string{"2006-01-02"},
fields: []string{"date"},
},
wantErr: false,
},
{
name: "simple case with error",
args: args{
format: []string{"2006-01-02"},
fields: []string{"date_bad"},
formats: []string{"2006-01-02"},
fields: []string{"date_bad"},
},
wantErr: false,
},
{
name: "mixed case",
args: args{
format: []string{"2006-01-02T15:04:05Z07:00", "2006-01-02"},
fields: []string{"date"},
formats: []string{"2006-01-02T15:04:05Z07:00", "2006-01-02"},
fields: []string{"date"},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := New("test", "test", "test")

fm, _ := frontmatter.Read(strings.NewReader(yml))

if err := b.DateFormat(fm, tt.args.format, tt.args.fields); (err != nil) != tt.wantErr {
t.Errorf("BuiltIns.DateFormat() error = %v, wantErr %v", err, tt.wantErr)
check := b.DateFormatFunc(tt.args.formats, tt.args.fields)
err := check(fm)

switch {
case tt.wantErr:
if err == nil {
t.Errorf("BuiltIns.DateFormatFunc() error = %v, wantErr %v", err, tt.wantErr)
return
}

assert.ErrorAs(t, err, &ValueErrors{})
case (err != nil) != tt.wantErr:
t.Errorf("BuiltIns.DateFormatFunc() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
Expand Down
18 changes: 11 additions & 7 deletions flint/builtins/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@ package builtins

import (
"fmt"
"strings"

"github.com/hay-kot/flint/pkgs/frontmatter"
"github.com/hay-kot/flint/pkgs/set"
)

func (b BuiltIns) EnumFunc(values []string, fields []string) CheckerFunc {
return func(fm frontmatter.FrontMatter) error {
return b.DateFormat(fm, values, fields)
}
}

func (b BuiltIns) Enum(fm frontmatter.FrontMatter, values []string, fields []string) error {
errGroup := ErrGroup{
valueErrors := ValueErrors{
ID: b.ID,
Level: b.Level,
Description: b.Description,
}

valuesSet := set.New(values...)
data := fm.Data()

for _, field := range fields {
v, ok := extractValue(data, strings.Split(field, "."))
v, ok := fm.Get(field)
if !ok {
continue
}

errAppender := func(v string) {
xy := fmtKeyCords(fm.KeyCords(field))

errGroup.Errors = append(errGroup.Errors, ErrGroupValue{
valueErrors.Errors = append(valueErrors.Errors, ValueError{
Line: xy,
Description: fmt.Sprintf("%q is not an allowed values", v),
Field: field,
Expand All @@ -51,8 +55,8 @@ func (b BuiltIns) Enum(fm frontmatter.FrontMatter, values []string, fields []str

}

if len(errGroup.Errors) > 0 {
return errGroup
if len(valueErrors.Errors) > 0 {
return valueErrors
}

return nil
Expand Down
19 changes: 15 additions & 4 deletions flint/builtins/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"

"github.com/hay-kot/flint/pkgs/frontmatter"
"github.com/stretchr/testify/assert"
)

func TestBuiltIns_Enum(t *testing.T) {

type args struct {
values []string
fields []string
Expand Down Expand Up @@ -54,11 +54,22 @@ func TestBuiltIns_Enum(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := New("test", "test", "test")

fm, _ := frontmatter.Read(strings.NewReader(yml))

if err := b.Enum(fm, tt.args.values, tt.args.fields); (err != nil) != tt.wantErr {
t.Errorf("BuiltIns.Enum() error = %v, wantErr %v", err, tt.wantErr)
check := b.EnumFunc(tt.args.values, tt.args.fields)
err := check(fm)

switch {
case tt.wantErr:
if err == nil {
t.Errorf("BuiltIns.EnumFunc() error = %v, wantErr %v", err, tt.wantErr)
return
}

assert.ErrorAs(t, err, &ValueErrors{})
case (err != nil) != tt.wantErr:
t.Errorf("BuiltIns.EnumFunc() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
Expand Down
18 changes: 0 additions & 18 deletions flint/builtins/errgroup.go

This file was deleted.

31 changes: 31 additions & 0 deletions flint/builtins/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package builtins

type ValueError struct {
Line string
Description string
Field string
}

type ValueErrors struct {
ID string
Level string
Description string
Errors []ValueError
}

func (m ValueErrors) Error() string {
return "match failed"
}

type FieldError = ValueError

type FieldErrors struct {
ID string
Level string
Description string
Fields []FieldError
}

func (e FieldErrors) Error() string {
return "required keys missing"
}
43 changes: 0 additions & 43 deletions flint/builtins/extractors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,8 @@ package builtins

import (
"fmt"

"github.com/hay-kot/flint/pkgs/set"
)

func extractKeys(mp map[string]any) *set.Set[string] {
keys := set.New[string]()
for k := range mp {
v := mp[k]

switch v := v.(type) {
case map[string]any:
for _, key := range extractKeys(v).Slice() {
keys.Insert(k + "." + key)
}
default:
keys.Insert(k)
}
}

return keys
}

func extractValue(mp map[string]any, parts []string) (any, bool) {
if len(parts) == 1 {
v, ok := mp[parts[0]]
if !ok {
return "", false
}

return v, true
}

v, ok := mp[parts[0]]
if !ok {
return "", false
}

switch v := v.(type) {
case map[string]any:
return extractValue(v, parts[1:])
default:
return "", false
}
}

func fmtKeyCords(x, y int) string {
if x == -1 {
return "0:0"
Expand Down

0 comments on commit 329a0b1

Please sign in to comment.