Skip to content

Commit ab6f574

Browse files
committed
typeAcceptor fixes
fixed "all not found" error with "all" types requested; empty types are now illegal;
1 parent bbb13fe commit ab6f574

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

internal/parser/input.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ func (fi InputFile) Ast() *ast.File { return fi.Pkg.Syntax[fi.SyntaxIdx] }
7878
type TypeAcceptor interface {
7979
// Should return true if type is required to be serializable
8080
Accepts(tspec *ast.TypeSpec) bool
81-
// should return true if no more types are expected
81+
// Should return true if no more types are expected
8282
IsDrained() bool
83+
// Should return true if acceptor has no type restrictions
84+
AcceptsAll() bool
8385
fmt.Stringer
8486
}
8587

@@ -88,7 +90,7 @@ func (fi InputFile) GetInputStructs(acceptor TypeAcceptor) ([]domain.InputStruct
8890
if err != nil {
8991
return nil, err
9092
}
91-
if !acceptor.IsDrained() {
93+
if !acceptor.IsDrained() && !acceptor.AcceptsAll() {
9294
return nil, fmt.Errorf("types '%s' were requested but not found", acceptor)
9395
}
9496
if len(structs) == 0 {

typeacceptor.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"go/ast"
2121

22+
"github.com/am4n0w4r/simser/internal/domain"
2223
"golang.org/x/exp/maps"
2324
)
2425

@@ -30,6 +31,9 @@ func newTypeAcceptor(rawTypeList []string) (*typeAcceptor, error) {
3031
}
3132
st := map[string]uint8{}
3233
for _, v := range rawTypeList {
34+
if v == "" {
35+
return nil, errors.Join(domain.ErrUnsupportedType, errors.New("empty type name in type list"))
36+
}
3337
st[v] = 0
3438
}
3539
return (*typeAcceptor)(&st), nil
@@ -47,11 +51,12 @@ func (st *typeAcceptor) Accepts(ts *ast.TypeSpec) bool {
4751
}
4852

4953
func (ts typeAcceptor) IsDrained() bool {
50-
_, hasAll := ts["all"]
51-
if len(ts) == 1 && hasAll {
52-
return false
53-
}
5454
return len(ts) == 0
5555
}
5656

57+
func (ta typeAcceptor) AcceptsAll() bool {
58+
_, hasAll := ta["all"]
59+
return len(ta) == 1 && hasAll
60+
}
61+
5762
func (ts typeAcceptor) String() string { return fmt.Sprintf("%s", maps.Keys(ts)) }

0 commit comments

Comments
 (0)