Skip to content
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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/ini/ini_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (l *iniLexer) tokenize(b []byte) ([]Token, error) {

for len(runes) > 0 && count < tokenAmount {
switch {
case isSubProperty(runes):
tokens[count], n, err = newLitToken(runes)
case isWhitespace(runes[0]):
tokens[count], n, err = newWSToken(runes)
case isComma(runes[0]):
Expand Down Expand Up @@ -101,6 +103,8 @@ func countTokens(runes []rune) int {

for len(runes) > 0 {
switch {
case isSubProperty(runes):
_, n, err = newLitToken(runes)
case isWhitespace(runes[0]):
_, n, err = newWSToken(runes)
case isComma(runes[0]):
Expand Down Expand Up @@ -128,6 +132,7 @@ func countTokens(runes []rune) int {
return count + 1
}


// Token indicates a metadata about a given value.
type Token struct {
t TokenType
Expand Down
31 changes: 19 additions & 12 deletions internal/ini/ini_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func TestParser(t *testing.T) {
regionLit, _, _ := newLitToken([]rune(`"us-west-2"`))
regionNoQuotesLit, _, _ := newLitToken([]rune("us-west-2"))

s3ServiceID, _, _ := newLitToken([]rune("s3"))
nestedParamsLit, _, _ := newLitToken([]rune("\n\tfoo=bar\n\tbar=baz\n"))

credentialID, _, _ := newLitToken([]rune("credential_source"))
ec2MetadataLit, _, _ := newLitToken([]rune("Ec2InstanceMetadata"))

Expand Down Expand Up @@ -57,6 +60,9 @@ func TestParser(t *testing.T) {
sepInValueExpr := newEqualExpr(newExpression(sepInValueID), equalOp)
sepInValueExpr.AppendChild(newExpression(sepInValueLit))

nestedEQExpr := newEqualExpr(newExpression(s3ServiceID), equalOp)
nestedEQExpr.AppendChild(newExpression(nestedParamsLit))

cases := []struct {
name string
r io.Reader
Expand Down Expand Up @@ -206,8 +212,9 @@ func TestParser(t *testing.T) {
},
{
name: "section statement",
r: bytes.NewBuffer([]byte(`[default]
region="us-west-2"`)),
r: bytes.NewBuffer([]byte(
`[default]
region="us-west-2"`)),
expectedStack: []AST{
newCompletedSectionStatement(
defaultProfileStmt,
Expand All @@ -217,15 +224,15 @@ func TestParser(t *testing.T) {
},
{
name: "complex section statement",
r: bytes.NewBuffer([]byte(`[default]
region = us-west-2
credential_source = Ec2InstanceMetadata
output = json
r: bytes.NewBuffer([]byte(
`[default]
region = us-west-2
credential_source = Ec2InstanceMetadata
output = json

[assumerole]
output = json
region = us-west-2
`)),
[assumerole]
output = json
region = us-west-2`)),
expectedStack: []AST{
newCompletedSectionStatement(
defaultProfileStmt,
Expand Down Expand Up @@ -258,7 +265,7 @@ region = us-west-2
newCompletedSectionStatement(
defaultProfileStmt,
),
newSkipStatement(newEqualExpr(newExpression(s3ID), equalOp)),
newExprStatement(nestedEQExpr),
newExprStatement(noQuotesRegionEQRegion),
newExprStatement(credEQExpr),
newExprStatement(outputEQExpr),
Expand Down Expand Up @@ -289,7 +296,7 @@ region = us-west-2
),
newExprStatement(noQuotesRegionEQRegion),
newExprStatement(credEQExpr),
newSkipStatement(newEqualExpr(newExpression(s3ID), equalOp)),
newExprStatement(nestedEQExpr),
newExprStatement(outputEQExpr),
newCompletedSectionStatement(
assumeProfileStmt,
Expand Down
71 changes: 68 additions & 3 deletions internal/ini/literal_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const (
NoneType = ValueType(iota)
StringType
QuotedStringType
// FUTURE(2226) MapType
Copy link
Contributor

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.

)

// Value is a union container
Expand All @@ -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) {
Expand All @@ -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])
}
Expand Down Expand Up @@ -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)
Expand All @@ -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")
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have gofmt running? It should clean up extra newlines like this.


// IntValue returns an integer value
func (v Value) IntValue() (int64, bool) {
i, err := strconv.ParseInt(string(v.raw), 0, 64)
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions internal/ini/testdata/valid/nested_fields_expected
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": {
Expand Down
2 changes: 1 addition & 1 deletion internal/ini/testdata/valid/op_sep_in_values_expected
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"key": "value5"
},
"case6": {
"s3": "",
"s3": "map[key:valuen6]",
"key": "=value6"
},
"case7": {
Expand Down
7 changes: 6 additions & 1 deletion internal/ini/walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ func TestValidDataFiles(t *testing.T) {
for k, v := range table {
switch e := v.(type) {
case string:
a := p.String(k)
var a string
if p.values[k].mp != nil {
a = fmt.Sprintf("%v", p.values[k].mp)
} else {
a = p.String(k)
}
if e != a {
t.Errorf("%s: expected %v, but received %v for profile %v", path, e, a, profile)
}
Expand Down
Loading