Skip to content

Commit

Permalink
add Error Checks in the Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
switchupcb committed Oct 9, 2021
1 parent f326db8 commit 4e71cc9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
7 changes: 3 additions & 4 deletions cli/parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p *Parser) astLocateTypeDecl(ltr *Locater) (*TypeDeclaration, error) {
// check the setup file.
if ltr.SetupFile.Name.Name == ltr.Package {
ts, _ = astTypeSearch(ltr.SetupFile, ltr.Definition)
if ts != nil && p.LastLocated != nil {
if ts != nil {
return &TypeDeclaration{
Package: p.LastLocated,
File: ltr.SetupFile,
Expand All @@ -74,7 +74,6 @@ func (p *Parser) astLocateTypeDecl(ltr *Locater) (*TypeDeclaration, error) {

// check the imports of the setup file.
for _, setImport := range ltr.SetupFile.Imports {

// use the exact import (by skipping non-matches) in the case of an alias (i.e `c` in `c "strconv"`)
if setImport.Name != nil && setImport.Name.Name != ltr.Package {
continue
Expand Down Expand Up @@ -132,10 +131,10 @@ func astTypeSearch(file *ast.File, typename string) (*ast.TypeSpec, error) {

// parsedDefinition represents the result of a parsed definition.
type parsedDefinition struct {
err error
imprt string
pkg string
typename string
err error
}

// parseDefinition determines the actual import, package, and name of a field based on its *types.Var definition.
Expand All @@ -160,7 +159,7 @@ func (p *Parser) parseDefinition(definition string) parsedDefinition {
pd.pkg = pkg.Name
}
if pd.pkg == "" {
pd.err = fmt.Errorf("an error occurred determining the package of definition %q.", definition)
pd.err = fmt.Errorf("an error occurred determining the package of definition %q", definition)
return pd
}

Expand Down
6 changes: 2 additions & 4 deletions cli/parser/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ type FieldSearch struct {
// The file that holds the type declaration for the field being searched.
DecFile *ast.File

// The filepath of the declaration file.
Filepath string

// The options that pertain to a field (and its subfields).
Options []Option

Expand Down Expand Up @@ -95,6 +92,7 @@ func (p *Parser) astSubfieldSearch(fs *FieldSearch, typefield *models.Field) ([]
case *types.Struct:
for i := 0; i < x.NumFields(); i++ {
xField := x.Field(i)

// create a new typefield if a subfield is a custom type
if (fs.MaxDepth == 0 || fs.Depth < fs.MaxDepth) && !isBasic(xField.Type()) {
parsedDefinition := p.parseDefinition(xField.Type().String())
Expand All @@ -114,10 +112,10 @@ func (p *Parser) astSubfieldSearch(fs *FieldSearch, typefield *models.Field) ([]
Depth: fs.Depth + 1,
MaxDepth: fs.MaxDepth,
})

if err != nil {
return nil, err
}

subfields = append(subfields, subfield)
} else {
subfield := &models.Field{
Expand Down
23 changes: 14 additions & 9 deletions cli/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ type Parser struct {
// The ast.Node of the `type Copygen Interface`.
Copygen *ast.InterfaceType

// The setup filepath.
Setpath string

// The option-comments parsed in the OptionMap.
Comments []*ast.Comment
// A key value cache used to reduce the amount of package load operations during a field search.
pkgcache map[string][]*packages.Package

// The last package to be loaded by a Locater.
LastLocated *packages.Package

// A key value cache used to reduce the amount of package load operations during a field search.
pkgcache map[string][]*packages.Package

// A key value cache used to reduce the amount of AST operations during a field search.
fieldcache map[string]*models.Field

// The setup filepath.
Setpath string

// The option-comments parsed in the OptionMap.
Comments []*ast.Comment
}

// Parse parses a generator's setup file.
Expand Down Expand Up @@ -69,11 +69,16 @@ func Parse(gen *models.Generator) error {
return err
}
for _, pkg := range pkgs {
if p.SetupFile.Name.Name == pkg.Name {
if p.SetupFile.Name == nil {
return fmt.Errorf("the setup file must declare a package: %v", p.Setpath)
} else if p.SetupFile.Name.Name == pkg.Name {
p.LastLocated = pkg
break
}
}
if p.LastLocated == nil {
return fmt.Errorf("the setup file's package could not be loaded correctly: %v", p.Setpath)
}

// Traverse the Abstract Syntax Tree.
err = p.Traverse(gen)
Expand Down

0 comments on commit 4e71cc9

Please sign in to comment.