-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for escaping . in address
- Loading branch information
Showing
17 changed files
with
320 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package editor | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func createAddressFromString(address string) []string { | ||
var separator byte = '.' | ||
var escapeString byte = '\\' | ||
|
||
var result []string | ||
var token []byte | ||
for i := 0; i < len(address); i++ { | ||
if address[i] == separator { | ||
result = append(result, string(token)) | ||
token = token[:0] | ||
} else if address[i] == escapeString && i+1 < len(address) { | ||
i++ | ||
token = append(token, address[i]) | ||
} else { | ||
token = append(token, address[i]) | ||
} | ||
} | ||
result = append(result, string(token)) | ||
return result | ||
} | ||
|
||
func createStringFromAddress(address []string) string { | ||
separator := "." | ||
escapeString := "\\" | ||
|
||
result := "" | ||
|
||
for i, s := range address { | ||
if i > 0 { | ||
result = result + separator | ||
} | ||
result = result + strings.ReplaceAll(s, separator, escapeString+separator) | ||
} | ||
|
||
return result | ||
} |
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,75 @@ | ||
package editor | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestCreateAddressFromString(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
address string | ||
want []string | ||
}{ | ||
{ | ||
name: "simple address", | ||
address: "b1", | ||
want: []string{"b1"}, | ||
}, | ||
{ | ||
name: "attribute address", | ||
address: "b1.l1.a1", | ||
want: []string{"b1", "l1", "a1"}, | ||
}, | ||
{ | ||
name: "escaped address", | ||
address: `b1.l\.1.a1`, | ||
want: []string{"b1", "l.1", "a1"}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := createAddressFromString(tc.address) | ||
|
||
if diff := cmp.Diff(tc.want, got); diff != "" { | ||
t.Fatalf("got:\n%s\nwant:\n%s\ndiff(-want +got):\n%v", got, tc.want, diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCreateStringFromAddress(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
address []string | ||
want string | ||
}{ | ||
{ | ||
name: "simple address", | ||
address: []string{"b1"}, | ||
want: "b1", | ||
}, | ||
{ | ||
name: "simple address", | ||
address: []string{"b1", "l1", "a1"}, | ||
want: "b1.l1.a1", | ||
}, | ||
{ | ||
name: "simple address", | ||
address: []string{"b1", `l.1`, "a1"}, | ||
want: `b1.l\.1.a1`, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := createStringFromAddress(tc.address) | ||
|
||
if diff := cmp.Diff(tc.want, got); diff != "" { | ||
t.Fatalf("got:\n%s\nwant:\n%s\ndiff(-want +got):\n%v", got, tc.want, diff) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.