Skip to content

Commit

Permalink
Merge 2cbff0d into 39fdcd9
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyqs committed Jan 31, 2019
2 parents 39fdcd9 + 2cbff0d commit 7096146
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 6 deletions.
7 changes: 7 additions & 0 deletions conf/includes/cluster.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"routes": [
"nats://nats-A:6222",
"nats://nats-C:6222",
"nats://nats-B:6222"
]
}
7 changes: 7 additions & 0 deletions conf/includes/multiple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"debug": true,
"include": "simple.json",
"cluster": {
"include": "cluster.json"
}
}
8 changes: 8 additions & 0 deletions conf/includes/multiple2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

'debug': true

'include': 'simple.json'

'cluster': {
'include': 'cluster.json'
}
8 changes: 8 additions & 0 deletions conf/includes/multiple2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

'debug': true

'include': 'simple.json'

'cluster': {
'include': 'cluster.json'
}
3 changes: 3 additions & 0 deletions conf/includes/simple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"listen": "127.0.0.1:4223"
}
24 changes: 18 additions & 6 deletions conf/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ func lexKeyStart(lx *lexer) stateFn {
func lexDubQuotedKey(lx *lexer) stateFn {
r := lx.peek()
if r == dqStringEnd {
lx.emit(itemKey)
include := lx.keyCheckKeyword(nil, nil)
if include != nil {
return include
}
lx.next()
return lexSkip(lx, lexKeyEnd)
} else if r == eof {
Expand All @@ -361,7 +364,10 @@ func lexDubQuotedKey(lx *lexer) stateFn {
func lexQuotedKey(lx *lexer) stateFn {
r := lx.peek()
if r == sqStringEnd {
lx.emit(itemKey)
include := lx.keyCheckKeyword(nil, nil)
if include != nil {
return include
}
lx.next()
return lexSkip(lx, lexKeyEnd)
} else if r == eof {
Expand Down Expand Up @@ -394,7 +400,7 @@ func (lx *lexer) keyCheckKeyword(fallThrough, push stateFn) stateFn {
// lexIncludeStart will consume the whitespace til the start of the value.
func lexIncludeStart(lx *lexer) stateFn {
r := lx.next()
if isWhitespace(r) {
if isWhitespace(r) || r == dqStringEnd || r == sqStringEnd || isKeySeparator(r) {
return lexSkip(lx, lexIncludeStart)
}
lx.backup()
Expand Down Expand Up @@ -441,7 +447,7 @@ func lexIncludeString(lx *lexer) stateFn {
lx.backup()
lx.emit(itemInclude)
return lx.pop()
case r == sqStringEnd:
case r == sqStringEnd || r == dqStringEnd:
lx.backup()
lx.emit(itemInclude)
lx.next()
Expand Down Expand Up @@ -668,7 +674,10 @@ func lexMapKeyStart(lx *lexer) stateFn {
func lexMapQuotedKey(lx *lexer) stateFn {
r := lx.peek()
if r == sqStringEnd {
lx.emit(itemKey)
include := lx.keyCheckKeyword(nil, lexMapValueEnd)
if include != nil {
return include
}
lx.next()
return lexSkip(lx, lexMapKeyEnd)
}
Expand All @@ -680,7 +689,10 @@ func lexMapQuotedKey(lx *lexer) stateFn {
func lexMapDubQuotedKey(lx *lexer) stateFn {
r := lx.peek()
if r == dqStringEnd {
lx.emit(itemKey)
include := lx.keyCheckKeyword(nil, lexMapValueEnd)
if include != nil {
return include
}
lx.next()
return lexSkip(lx, lexMapKeyEnd)
}
Expand Down
35 changes: 35 additions & 0 deletions conf/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,38 @@ func TestIncludeVariablesWithChecks(t *testing.T) {
expectKeyVal(t, m, "BOB_PASS", "$2a$11$dZM98SpGeI7dCFFGSpt.JObQcix8YHml4TBUZoge9R1uxnMIln5ly", 3, 1)
expectKeyVal(t, m, "CAROL_PASS", "foo", 6, 3)
}

func TestIncludesJSONSyntax(t *testing.T) {
ex := map[string]interface{}{
"listen": "127.0.0.1:4223",
"debug": true,
"cluster": map[string]interface{}{
"routes": []interface{}{
"nats://nats-A:6222",
"nats://nats-C:6222",
"nats://nats-B:6222",
},
},
}
m, err := ParseFile("includes/multiple.json")
if err != nil {
t.Fatalf("Received err: %v\n", err)
}
if m == nil {
t.Fatal("Received nil map")
}
if !reflect.DeepEqual(m, ex) {
t.Fatalf("Not Equal:\nReceived: '%+v'\nExpected: '%+v'\n", m, ex)
}

m2, err := ParseFile("includes/multiple2.conf")
if err != nil {
t.Fatalf("Received err: %v\n", err)
}
if m2 == nil {
t.Fatal("Received nil map")
}
if !reflect.DeepEqual(m2, ex) {
t.Fatalf("Not Equal:\nReceived: '%+v'\nExpected: '%+v'\n", m2, ex)
}
}

0 comments on commit 7096146

Please sign in to comment.