-
Notifications
You must be signed in to change notification settings - Fork 656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds subproperty as new literal type #2308
Closed
isaiahvita
wants to merge
8
commits into
aws:feat-endpoint-config
from
isaiahvita:extender-lexer-isvita
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
093ad39
works on test file, unit tests failing
isaiahvita 828047e
ini and config tests passing
isaiahvita ace1cfe
add comments, add extra op
isaiahvita ad797ea
remove added newline
isaiahvita c75bc0f
fix casing
isaiahvita 1bbd8a5
fix casing 2
isaiahvita 1bc1c8f
add new literal value map type
isaiahvita 0968d2a
remove unnecessary comment
isaiahvita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -60,7 +60,6 @@ const ( | |
NoneType = ValueType(iota) | ||
StringType | ||
QuotedStringType | ||
// FUTURE(2226) MapType | ||
) | ||
|
||
// Value is a union container | ||
|
@@ -69,7 +68,7 @@ type Value struct { | |
raw []rune | ||
|
||
str string | ||
// FUTURE(2226) mp map[string]string | ||
mp map[string]string | ||
} | ||
|
||
func newValue(t ValueType, base int, raw []rune) (Value, error) { | ||
|
@@ -81,6 +80,21 @@ func newValue(t ValueType, base int, raw []rune) (Value, error) { | |
switch t { | ||
case StringType: | ||
v.str = string(raw) | ||
if isSubProperty(raw) { | ||
newlineParts := strings.Split(string(raw), "\n") | ||
for _, part := range newlineParts { | ||
operandParts := strings.Split(part, "=") | ||
if len(operandParts) < 2 { | ||
continue | ||
} | ||
key := strings.TrimSpace(operandParts[0]) | ||
val := strings.TrimSpace(operandParts[1]) | ||
if v.mp == nil { | ||
v.mp = make(map[string]string) | ||
} | ||
v.mp[key] = val | ||
} | ||
} | ||
case QuotedStringType: | ||
v.str = string(raw[1 : len(raw)-1]) | ||
} | ||
|
@@ -114,8 +128,15 @@ func newLitToken(b []rune) (Token, int, error) { | |
if err != nil { | ||
return token, n, err | ||
} | ||
|
||
token = newToken(TokenLit, b[:n], QuotedStringType) | ||
} else if isSubProperty(b) { | ||
offset := 0 | ||
end, err := getSubProperty(b, offset) | ||
if err != nil { | ||
return token, n, err | ||
} | ||
token = newToken(TokenLit, b[offset:end], StringType) | ||
n = end | ||
} else { | ||
n, err = getValue(b) | ||
token = newToken(TokenLit, b[:n], StringType) | ||
|
@@ -124,6 +145,49 @@ func newLitToken(b []rune) (Token, int, error) { | |
return token, n, err | ||
} | ||
|
||
func isSubProperty(runes []rune) bool { | ||
// needs at least | ||
// (1) newline (2) whitespace (3) literal | ||
if len(runes) < 3 { | ||
return false | ||
} | ||
|
||
// must have an equal expression | ||
split := strings.FieldsFunc(string(runes), func(r rune)(bool){ | ||
return isOp([]rune{r}) | ||
}) | ||
if len(split) < 2 { | ||
return false | ||
} | ||
|
||
// must start with a new line | ||
if !isNewline(runes) { | ||
return false | ||
} | ||
_, n, err := newNewlineToken(runes) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
// whitespace must follow newline | ||
return isWhitespace(runes[n]) | ||
} | ||
|
||
// getSubProperty pulls all subproperties and terminates when | ||
// it hits a newline that is not the start of another subproperty. | ||
// offset allows for removal of leading newline and whitespace | ||
// characters | ||
func getSubProperty(runes []rune, offset int) (int, error) { | ||
for idx, val := range runes[offset:] { | ||
if val == '\n' && !isSubProperty(runes[offset+idx:]) { | ||
return offset + idx, nil | ||
} | ||
} | ||
return 0, fmt.Errorf("no sub property") | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have |
||
|
||
// IntValue returns an integer value | ||
func (v Value) IntValue() (int64, bool) { | ||
i, err := strconv.ParseInt(string(v.raw), 0, 64) | ||
|
@@ -165,6 +229,7 @@ func isTrimmable(r rune) bool { | |
// StringValue returns the string value | ||
func (v Value) StringValue() string { | ||
switch v.Type { | ||
|
||
case StringType: | ||
return strings.TrimFunc(string(v.raw), isTrimmable) | ||
case QuotedStringType: | ||
|
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"foo": { | ||
"aws_access_key_id": "map[aws_secret_access_key:valid]", | ||
"aws_session_token": "valid" | ||
}, | ||
"bar": { | ||
|
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
"key": "value5" | ||
}, | ||
"case6": { | ||
"s3": "", | ||
"s3": "map[key:valuen6]", | ||
"key": "=value6" | ||
}, | ||
"case7": { | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why no
MapType
? I don't fully remember what all uses this, though.