From ee6653c2895ab81d57433c7c1a2bbcff9f368e01 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 16 Nov 2016 22:47:43 +0000 Subject: [PATCH] Improve SQL DEFINE SCOPE statement --- sql/ast.go | 14 ++++++++------ sql/cork.go | 8 ++++++-- sql/scope.go | 37 ++++++++++++++++++++++++++++++++----- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/sql/ast.go b/sql/ast.go index 44dd04e8..76af26e5 100644 --- a/sql/ast.go +++ b/sql/ast.go @@ -256,12 +256,14 @@ type RemoveTokenStatement struct { // DefineScopeStatement represents an SQL DEFINE SCOPE statement. type DefineScopeStatement struct { - KV string `cork:"-" codec:"-"` - NS string `cork:"-" codec:"-"` - DB string `cork:"-" codec:"-"` - Name string `cork:"name" codec:"name"` - Time time.Duration `cork:"time" codec:"time"` - Opts interface{} `cork:"opts" codec:"opts"` + KV string `cork:"-" codec:"-"` + NS string `cork:"-" codec:"-"` + DB string `cork:"-" codec:"-"` + Name string `cork:"name" codec:"name"` + Time time.Duration `cork:"time" codec:"time"` + Signup Expr `cork:"signup" codec:"signup"` + Signin Expr `cork:"signin" codec:"signin"` + Policy map[string]interface{} `cork:"policy" codec:"policy"` } // RemoveScopeStatement represents an SQL REMOVE SCOPE statement. diff --git a/sql/cork.go b/sql/cork.go index 5e52384e..1665412c 100644 --- a/sql/cork.go +++ b/sql/cork.go @@ -1199,7 +1199,9 @@ func (this *DefineScopeStatement) MarshalCORK() (dst []byte, err error) { e := cork.NewEncoder(b) e.Encode(this.Name) e.Encode(this.Time) - e.Encode(this.Opts) + e.Encode(this.Signup) + e.Encode(this.Signin) + e.Encode(this.Policy) return b.Bytes(), nil } @@ -1208,7 +1210,9 @@ func (this *DefineScopeStatement) UnmarshalCORK(src []byte) (err error) { d := cork.NewDecoder(b) d.Decode(&this.Name) d.Decode(&this.Time) - d.Decode(&this.Opts) + d.Decode(&this.Signup) + d.Decode(&this.Signin) + d.Decode(&this.Policy) return } diff --git a/sql/scope.go b/sql/scope.go index bda113f1..a8cfdc2a 100644 --- a/sql/scope.go +++ b/sql/scope.go @@ -26,12 +26,39 @@ func (p *parser) parseDefineScopeStatement() (stmt *DefineScopeStatement, err er return nil, err } - if _, _, err = p.shouldBe(SESSION); err != nil { - return nil, err - } + for { + + tok, _, exi := p.mightBe(SESSION, POLICY, SIGNUP, SIGNIN) + if !exi { + break + } + + if p.is(tok, SESSION) { + if stmt.Time, err = p.parseDuration(); err != nil { + return nil, err + } + } + + if p.is(tok, POLICY) { + if stmt.Policy, err = p.parseObject(); err != nil { + return nil, err + } + } + + if p.is(tok, SIGNUP) { + _, _, _ = p.mightBe(AS) + if stmt.Signup, err = p.parseExpr(); err != nil { + return nil, err + } + } + + if p.is(tok, SIGNIN) { + _, _, _ = p.mightBe(AS) + if stmt.Signin, err = p.parseExpr(); err != nil { + return nil, err + } + } - if stmt.Time, err = p.parseDuration(); err != nil { - return nil, err } if _, _, err = p.shouldBe(EOF, SEMICOLON); err != nil {