From 3ef0c2329ab35f68911c90fbc34870e323fc9f5f Mon Sep 17 00:00:00 2001 From: Derek Collison Date: Sat, 27 Jul 2013 13:32:57 -0700 Subject: [PATCH] Fixed bug with block comments inside maps --- conf/lex.go | 15 +++++++++++++-- conf/parse.go | 2 +- conf/parse_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/conf/lex.go b/conf/lex.go index 5c9905bb..5bb64f71 100644 --- a/conf/lex.go +++ b/conf/lex.go @@ -408,6 +408,18 @@ func lexMapKeyStart(lx *lexer) stateFn { case r == mapEnd: lx.next() return lexSkip(lx, lexMapEnd) + case r == commentHashStart: + lx.next() + lx.push(lexMapKeyStart) + return lexCommentStart + case r == commentSlashStart: + lx.next() + rn := lx.next() + if rn == commentSlashStart { + lx.push(lexMapKeyStart) + return lexCommentStart + } + lx.backup() } lx.ignore() lx.next() @@ -462,8 +474,7 @@ func lexMapValue(lx *lexer) stateFn { lx.backup() fallthrough case r == mapValTerm: - return lx.errorf("Unexpected map value terminator '%s'.", - mapValTerm) + return lx.errorf("Unexpected map value terminator '%s'.", mapValTerm) case r == mapEnd: return lexSkip(lx, lexMapEnd) } diff --git a/conf/parse.go b/conf/parse.go index 885d66d3..3aa466cf 100644 --- a/conf/parse.go +++ b/conf/parse.go @@ -174,7 +174,7 @@ func (p *parser) setValue(val interface{}) { // Map processing if ctx, ok := p.ctx.(map[string]interface{}); ok { key := p.popKey() - // FIXME(dlc), make sure error if redefining same key? + // FIXME(dlc), make sure to error if redefining same key? ctx[key] = val } } diff --git a/conf/parse_test.go b/conf/parse_test.go index 57077a3f..3cc8bb6e 100644 --- a/conf/parse_test.go +++ b/conf/parse_test.go @@ -53,3 +53,46 @@ func TestSample1(t *testing.T) { } test(t, sample1, ex) } + +var cluster = ` +cluster { + port: 4244 + + authorization { + user: route_user + password: top_secret + timeout: 1 + } + + # Routes are actively solicited and connected to from this server. + # Other servers can connect to us if they supply the correct credentials + # in their routes definitions from above. + + // Test both styles of comments + + routes = [ + nats-route://foo:bar@apcera.me:4245 + nats-route://foo:bar@apcera.me:4246 + ] +} +` + +func TestSample2(t *testing.T) { + ex := map[string]interface{} { + "cluster": map[string]interface{} { + "port": int64(4244), + "authorization": map[string]interface{} { + "user": "route_user", + "password": "top_secret", + "timeout": int64(1), + }, + "routes": []interface{} { + "nats-route://foo:bar@apcera.me:4245", + "nats-route://foo:bar@apcera.me:4246", + }, + }, + } + + test(t, cluster, ex) +} +