Skip to content

Commit

Permalink
Merge pull request #12 from takaishi/fix/scanner
Browse files Browse the repository at this point in the history
fix: scanner
  • Loading branch information
takaishi authored Aug 31, 2024
2 parents 4d16280 + c1ae476 commit 6d284c2
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 11 deletions.
15 changes: 11 additions & 4 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ func (app *App) cutImportBlock(data []byte, to string, id string) ([]byte, error
return ch == '-' || ch == '_' || ch == '.' || ch == '[' || ch == ']' || ch == '"' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
}

var lastPos int
var inImportBlock bool

for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
switch s.TokenText() {
case "import":
fmt.Printf("import block found\n")
spos = s.Offset
if !inImportBlock {
if s.TokenText() == "import" && isAtLineStart(data, lastPos, s.Position.Offset) {
spos = s.Offset
inImportBlock = true
lastPos = s.Position.Offset
}
} else {
var importBlock ImportBlock
var current string
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
Expand Down Expand Up @@ -89,6 +95,7 @@ func (app *App) cutImportBlock(data []byte, to string, id string) ([]byte, error
}
}
}
lastPos = s.Position.Offset
}

return nil, nil
Expand Down
19 changes: 19 additions & 0 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ bbb
want: []byte("\naaa\nbbb\n"),
wantErr: false,
},
{
name: "",
fields: fields{},
args: args{
data: []byte(`
# import
aaa
import {
id = "resource_id"
to = module.foo.hoge
}
bbb
`),
to: "module.foo.hoge",
id: "resource_id",
},
want: []byte("\n# import\naaa\nbbb\n"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
14 changes: 11 additions & 3 deletions moved.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,17 @@ func (app *App) cutMovedBlock(data []byte, to string, from string) ([]byte, erro
return ch == '-' || ch == '_' || ch == '.' || ch == '"' || ch == '[' || ch == ']' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
}

var lastPos int
var inMovedBlock bool

for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
switch s.TokenText() {
case "moved":
spos = s.Offset
if !inMovedBlock {
if s.TokenText() == "moved" && isAtLineStart(data, lastPos, s.Position.Offset) {
spos = s.Offset
inMovedBlock = true
lastPos = s.Position.Offset
}
} else {
var movedBlock MoveBlock
var current string
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
Expand Down Expand Up @@ -122,6 +129,7 @@ func (app *App) cutMovedBlock(data []byte, to string, from string) ([]byte, erro
}
}
}
lastPos = s.Position.Offset
}

return nil, nil
Expand Down
19 changes: 19 additions & 0 deletions moved_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ bbb
want: []byte("\naaa\nbbb\n"),
wantErr: false,
},
{
name: "",
fields: fields{},
args: args{
data: []byte(`
# moved
aaa
moved {
from = module.foo.hoge
to = module.foo.piyo
}
bbb
`),
from: "module.foo.hoge",
to: "module.foo.piyo",
},
want: []byte("\n# moved\naaa\nbbb\n"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
23 changes: 19 additions & 4 deletions removed.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,20 @@ func (app *App) cutRemovedBlock(data []byte, from string) ([]byte, error) {
return ch == '-' || ch == '_' || ch == '.' || ch == '"' || ch == '[' || ch == ']' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
}

var lastPos int
var inRemovedBlock bool

for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
switch s.TokenText() {
case "removed":
spos = s.Offset
if !inRemovedBlock {
if s.TokenText() == "removed" && isAtLineStart(data, lastPos, s.Position.Offset) {
spos = s.Offset
inRemovedBlock = true
lastPos = s.Position.Offset
}
} else {
movedBlock := &RemovedBlock{}
var current string
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
fmt.Println(s.TokenText())
switch s.TokenText() {
case "{":
// Ignore
Expand All @@ -92,6 +98,7 @@ func (app *App) cutRemovedBlock(data []byte, from string) ([]byte, error) {
data = bytes.Join([][]byte{data[:spos], data[epos:]}, []byte(""))
return data, nil
}

case "from":
current = "from"
case "lifecycle":
Expand All @@ -112,6 +119,7 @@ func (app *App) cutRemovedBlock(data []byte, from string) ([]byte, error) {
}
}
}
lastPos = s.Position.Offset
}

return nil, nil
Expand Down Expand Up @@ -142,3 +150,10 @@ func (app *App) readLifecycleBlock(s *scanner.Scanner) (*LifecycleBlock, error)

return lifecycleBlock, nil
}

func isAtLineStart(data []byte, lastPos int, currentPos int) bool {
if lastPos == 0 {
return true
}
return bytes.LastIndexByte(data[lastPos:currentPos], '\n') == len(data[lastPos:currentPos])-1
}
20 changes: 20 additions & 0 deletions removed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ bbb
want: []byte("\naaa\nbbb\n"),
wantErr: false,
},
{
name: "",
fields: fields{},
args: args{
data: []byte(`
# removed
aaa
removed {
from = module.foo.hoge["aaa"]
lifecycle {
destroy = false
}
}
bbb
`),
from: "module.foo.hoge[\"aaa\"]",
},
want: []byte("\n# removed\naaa\nbbb\n"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 6d284c2

Please sign in to comment.