Skip to content

Commit

Permalink
Merge pull request #70 from xushiwei/ydb
Browse files Browse the repository at this point in the history
classApi; gop_autogen add generated header
  • Loading branch information
xushiwei authored Feb 9, 2024
2 parents 36e8dde + 37658ff commit 266efd7
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 66 deletions.
2 changes: 2 additions & 0 deletions demo/classfile_blog/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions demo/classfile_hello/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions demo/classfile_static/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions demo/classfile_statichttp/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 27 additions & 25 deletions ydb/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ type Class struct {

query *query // query

api *api
result []reflect.Value // result of an api call

ret func(args ...any) error
Expand Down Expand Up @@ -798,16 +797,33 @@ func (p *Class) Limit__2(n int, cond string, args ...any) error {

// -----------------------------------------------------------------------------

// Delete deltes rows by cond.
func (p *Class) Delete__0(src ast.Expr, cond string, args ...any) error {
panic("todo")
}

// Delete deltes rows by cond.
func (p *Class) Delete__1(cond string, args ...any) error {
return p.Delete__0(nil, cond, args...)
}

// -----------------------------------------------------------------------------

type classApi struct {
cls *Class
api *api
}

type api struct {
name string
spec any
}

// Api creates a new api by a spec.
func (p *Class) Api(name string, spec any, src ...ast.Expr) {
func (p *Class) Api(name string, spec any, src ...ast.Expr) func(args ...any) {
api := &api{name: name, spec: spec}
p.api = api
p.apis[name] = api
return classApi{p, api}.call
}

var (
Expand All @@ -822,13 +838,14 @@ func setRetErr(result []reflect.Value, errRet error) {
}
}

func (p *Class) doCall(args ...any) {
func (ca classApi) call(args ...any) {
vArgs := make([]reflect.Value, len(args))
for i, arg := range args {
vArgs[i] = reflect.ValueOf(arg)
}
vFn := reflect.ValueOf(p.api.spec)
vFn := reflect.ValueOf(ca.api.spec)

var p = ca.cls
var old = p.onErr
var errRet error
p.onErr = func(err error) {
Expand Down Expand Up @@ -857,26 +874,6 @@ func (p *Class) doCall(args ...any) {
p.result = vFn.Call(vArgs)
}

// Call calls an api with specified args.
func (p *Class) Call__0(src ast.Expr, args ...any) {
if p.api == nil {
log.Panicln("please call `call` after an api definition")
}
if src == nil {
if len(args) == 0 {
p.doCall(nil)
return
}
args = append(make([]any, 1, len(args)+1), args...)
}
p.doCall(args...)
}

// Call calls an api with specified args.
func (p *Class) Call__1(args ...any) {
p.doCall(args...)
}

func (p *Class) callRet(args ...any) error {
t := p.t()
result := p.result
Expand All @@ -894,4 +891,9 @@ func (p *Class) callRet(args ...any) error {
return nil
}

// Out returns the ith reuslt.
func (p *Class) Out(i int) any {
return p.result[i].Interface()
}

// -----------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion ydb/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

const (
GopPackage = true
GopPackage = "github.com/goplus/yap/test"
)

// -----------------------------------------------------------------------------
Expand Down
82 changes: 74 additions & 8 deletions ydb/demo/foo/article_ydb.gox
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,93 @@ table Tag, "v0.1.0"
class "Articles", => {
use "article"

api "add", func(doc *Article) error {
add := api("add", func(doc *Article) error {
insert doc
return nil
}
})

api "tags", func(docId string, tags ...string) {
}
doc1 := &Article{}
doc1.Id, doc1.Author, doc1.Title = "123", "abc", "title1"
add doc1
ret nil

api "listByTag", func(tag string) (result []ArticleEntry) {
add doc1
ret ErrDuplicated

doc2 := &Article{}
doc2.Id, doc2.Author, doc2.Title = "124", "efg", "title2"
add doc2
ret nil

doc3 := &Article{}
doc3.Id, doc3.Author, doc3.Title = "125", "efg", "title3"
add doc3
ret nil

doc4 := &Article{}
doc4.Id, doc4.Author, doc4.Title = "225", "abc", "title4"
add doc4
ret nil

doc5 := &Article{}
doc5.Id, doc5.Author, doc5.Title = "555", "abc", "title5"
add doc5
ret nil

get := api("get", func(docId string) (doc Article, err error) {
query "id=?", docId
ret &doc
return
})

get doc1.Id
ret doc1, nil

get doc2.Id
ret doc2, nil

// get "unknown"
// match out(1), ErrNoRows

setTags := api("setTags", func(docId string, tags ...string) error {
var oldtags []string
query "tag.doc=?", docId
ret "tag.name", &oldtags

tagsAdd, tagsDel := diff(tags, oldtags)
delete "tag.name=?", tagsDel
insert "doc", docId, "tag.name", tagsAdd
return nil
})

tags := api("tags", func(docId string) (tags []string, err error) {
query "tag.doc=?", docId
ret "tag.name", &tags
return
})
_ = tags

setTags doc1.Id, "tag1", "tag2"
ret nil

// tags doc1.Id
// equalSet out(0), ["tag1", "tag2"]

listByTag := api("listByTag", func(tag string) (result []ArticleEntry) {
var ids []string
query "tag.name=?", tag
ret "tag.doc", &ids

query "id=?", ids
ret &result
return
}
})
_ = listByTag

api "listByAuthor", func(author string) (result []ArticleEntry) {
listByAuthor := api("listByAuthor", func(author string) (result []ArticleEntry) {
query "author=?", author
ret &result
return
}
})
_ = listByAuthor
}
4 changes: 4 additions & 0 deletions ydb/demo/foo/foo.gop
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func Hs256(pwd, salt string) string {
b := hmac.new(sha256.New, []byte(salt)).sum([]byte(pwd))
return base64.RawURLEncoding.encodeToString(b)
}

func Diff(new, old []string) (add, del []string) {
return
}
Loading

0 comments on commit 266efd7

Please sign in to comment.