-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from s12chung/firm
Firm revision
- Loading branch information
Showing
55 changed files
with
2,524 additions
and
740 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"error": "db.NoteCreateParams.DictionarySource.Present: value is not present, db.NoteCreateParams.SourceName.Present: value is not present", | ||
"error": "db.NoteCreateParams.DictionarySource.Present: DictionarySource is not present, db.NoteCreateParams.SourceName.Present: SourceName is not present", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"error": "api.PartCreateOrUpdateRequest.Text.TrimPresent: value is just spaces or empty", | ||
"error": "api.PartCreateOrUpdateRequest.Text.TrimPresent: Text is just spaces or empty", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
5 changes: 5 additions & 0 deletions
5
pkg/api/testdata/TestRoutes_PartCreateMulti/51_parts_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"error": "api.PartCreateMultiRequest.Parts.Len-LessOrEqual: Parts attribute, Len, is not less than or equal to 20", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
2 changes: 1 addition & 1 deletion
2
pkg/api/testdata/TestRoutes_PartCreateMulti/empty_parts_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"error": "api.PartCreateMultiRequest.Parts.Present: value is not present", | ||
"error": "api.PartCreateMultiRequest.Parts.Present: Parts is not present", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"error": "api.PartCreateOrUpdateRequest.Text.TrimPresent: value is just spaces or empty", | ||
"error": "api.PartCreateOrUpdateRequest.Text.TrimPresent: Text is just spaces or empty", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"error": "api.PrePartListSignRequest.PreParts.Present: value is not present", | ||
"error": "api.PrePartListSignRequest.PreParts.Present: PreParts is not present", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
5 changes: 5 additions & 0 deletions
5
pkg/api/testdata/TestRoutes_SourceCreate/51_parts_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"error": "api.SourceCreateRequest.PartCreateMultiRequest.Parts.Len-LessOrEqual: Parts attribute, Len, is not less than or equal to 20", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
2 changes: 1 addition & 1 deletion
2
pkg/api/testdata/TestRoutes_SourceCreate/empty_parts_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"error": "api.SourceCreateRequest.Parts.Present: value is not present", | ||
"error": "api.SourceCreateRequest.PartCreateMultiRequest.Parts.Present: Parts is not present", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"error": "api.SourceUpdateRequest.Name.Present: value is not present", | ||
"error": "api.SourceUpdateRequest.Name.Present: Name is not present", | ||
"code": 422, | ||
"status_text": "Unprocessable Entity" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Package attr contains the default rule.Attributes | ||
package attr | ||
|
||
import "reflect" | ||
|
||
var intType = reflect.TypeOf(0) | ||
var stringType = reflect.TypeOf("") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package attr | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/s12chung/text2anki/pkg/firm" | ||
"github.com/s12chung/text2anki/pkg/firm/rule" | ||
) | ||
|
||
func testTypeCheck(t *testing.T, data any, ruleName, badCondition string, attr rule.Attribute) { | ||
require := require.New(t) | ||
|
||
typ := reflect.TypeOf(data) | ||
|
||
var ruleTypeError *firm.RuleTypeError | ||
if badCondition != "" { | ||
ruleTypeError = firm.NewRuleTypeError(ruleName, typ, badCondition) | ||
} | ||
require.Equal(ruleTypeError, attr.TypeCheck(typ)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package attr | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/s12chung/text2anki/pkg/firm" | ||
) | ||
|
||
// Len is a rule.Attribute that returns the reflect.Len attribute | ||
type Len struct{} | ||
|
||
// Name is the name of the Attribute | ||
func (l Len) Name() string { return "Len" } | ||
|
||
// Type is the type of the Attribute | ||
func (l Len) Type() reflect.Type { return intType } | ||
|
||
// Get gets the attribute value from the value | ||
func (l Len) Get(value reflect.Value) reflect.Value { return reflect.ValueOf(value.Len()) } | ||
|
||
// TypeCheck checks whether the type is valid for the Attribute | ||
func (l Len) TypeCheck(typ reflect.Type) *firm.RuleTypeError { | ||
//nolint:exhaustive // these are the only types that return nil | ||
switch typ.Kind() { | ||
case reflect.Slice, reflect.Array, reflect.Map, reflect.Chan, reflect.String: | ||
return nil | ||
case reflect.Ptr: | ||
// reflect.Len only handles pointers to Arrays | ||
if typ.Elem().Kind() == reflect.Array { | ||
return nil | ||
} | ||
} | ||
return firm.NewRuleTypeError(l.Name(), typ, "does not have a length (not a Slice, Array, Array pointer, Channel, Map or String)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package attr | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLen_Type(t *testing.T) { | ||
require.Equal(t, reflect.TypeOf(0), Len{}.Type()) | ||
} | ||
|
||
func TestLen_Get(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
data any | ||
result int | ||
}{ | ||
{name: "slice", data: []int{1, 2}, result: 2}, | ||
{name: "array", data: [3]int{1, 2, 3}, result: 3}, | ||
{name: "array_pointer", data: &[3]int{1, 2, 3}, result: 3}, | ||
{name: "map", data: map[int]int{1: 1, 2: 2}, result: 2}, | ||
{name: "channel", data: make(chan int), result: 0}, | ||
{name: "string", data: "abc", result: 3}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
require := require.New(t) | ||
require.Equal(reflect.ValueOf(tc.result), Len{}.Get(reflect.ValueOf(tc.data))) | ||
}) | ||
} | ||
} | ||
|
||
func TestLen_TypeCheck(t *testing.T) { | ||
badCondition := "does not have a length (not a Slice, Array, Array pointer, Channel, Map or String)" | ||
|
||
tcs := []struct { | ||
name string | ||
data any | ||
badCondition string | ||
}{ | ||
{name: "slice", data: []int{1, 2}}, | ||
{name: "slice_pointer", data: &[]int{1, 2}, badCondition: badCondition}, | ||
{name: "array", data: [3]int{1, 2, 3}}, | ||
{name: "array_pointer", data: &[3]int{1, 2, 3}}, | ||
{name: "map", data: map[int]int{1: 1, 2: 2}}, | ||
{name: "map_pointer", data: &map[int]int{1: 1, 2: 2}, badCondition: badCondition}, | ||
{name: "channel", data: make(chan int)}, | ||
{name: "string", data: "abc"}, | ||
{name: "int", data: 0, badCondition: badCondition}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
testTypeCheck(t, tc.data, "Len", tc.badCondition, Len{}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.