From ae30ef8295222d9b4a73ec1c511ea4d3bfa60894 Mon Sep 17 00:00:00 2001 From: RPJosh Date: Fri, 26 Apr 2024 20:57:06 +0200 Subject: [PATCH] Fix parsing of existing imports with empty line --- structt/struct.go | 12 +++++++++--- structt/struct_test.go | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/structt/struct.go b/structt/struct.go index a541168..12272ab 100644 --- a/structt/struct.go +++ b/structt/struct.go @@ -457,7 +457,7 @@ func (c *constructor) patchImports(existingContent string, imports map[string]bo } // Regex to find an import statement - reg := regexp.MustCompile(`"([^"]+)"`) + importRegex := regexp.MustCompile(`"([^"]+)"`) var importStart, importEnd int importFound := true @@ -481,7 +481,7 @@ func (c *constructor) patchImports(existingContent string, imports map[string]bo continue } else { // Extract imported package - matches := reg.FindStringSubmatch(lineContent) + matches := importRegex.FindStringSubmatch(lineContent) if len(matches) >= 2 { // We can only import ONE package without () if _, exists := imports[matches[1]]; !exists { @@ -505,7 +505,13 @@ func (c *constructor) patchImports(existingContent string, imports map[string]bo break } - matches := reg.FindStringSubmatch(lineContent) + // Ignore any empty import lines. It's used as a seperator between std packages + // and "external" modules + if lineContent == "" { + continue + } + + matches := importRegex.FindStringSubmatch(lineContent) if len(matches) >= 2 { if _, exists := imports[matches[1]]; !exists { imports[matches[1]] = true diff --git a/structt/struct_test.go b/structt/struct_test.go index 90fd45b..4de5ffc 100644 --- a/structt/struct_test.go +++ b/structt/struct_test.go @@ -328,6 +328,8 @@ package olaf import ( "time" "database/sql" + + "git.anything" ) type SomeRandomTab struct { @@ -349,6 +351,7 @@ package olaf import ( "database/sql" + "git.anything" "sql.NullString" "time" )