datasource.gno
1.61 Kb · 76 lines
1package hor
2
3import (
4 "errors"
5
6 "gno.land/p/demo/avl"
7 "gno.land/p/demo/ufmt"
8 "gno.land/p/jeronimoalbi/datasource"
9)
10
11func NewDatasource() Datasource {
12 return Datasource{exhibition}
13}
14
15type Datasource struct {
16 exhibition *Exhibition
17}
18
19func (ds Datasource) Size() int { return ds.exhibition.items.Size() }
20
21func (ds Datasource) Records(q datasource.Query) datasource.Iterator {
22 return &iterator{
23 exhibition: ds.exhibition,
24 index: q.Offset,
25 maxIndex: q.Offset + q.Count,
26 }
27}
28
29func (ds Datasource) Record(id string) (datasource.Record, error) {
30 v, found := ds.exhibition.items.Get(id)
31 if !found {
32 return nil, errors.New("realm submission not found")
33 }
34 return record{v.(*Item)}, nil
35}
36
37type record struct {
38 item *Item
39}
40
41func (r record) ID() string { return r.item.id.String() }
42func (r record) String() string { return r.item.pkgpath }
43
44func (r record) Fields() (datasource.Fields, error) {
45 fields := avl.NewTree()
46 fields.Set(
47 "details",
48 ufmt.Sprintf("Votes: ⏶ %d - ⏷ %d", r.item.upvote.Size(), r.item.downvote.Size()),
49 )
50 return fields, nil
51}
52
53func (r record) Content() (string, error) {
54 content := r.item.Render(false)
55 return content, nil
56}
57
58type iterator struct {
59 exhibition *Exhibition
60 index, maxIndex int
61 record *record
62}
63
64func (it iterator) Record() datasource.Record { return it.record }
65func (it iterator) Err() error { return nil }
66
67func (it *iterator) Next() bool {
68 if it.index >= it.maxIndex || it.index >= it.exhibition.items.Size() {
69 return false
70 }
71
72 _, v := it.exhibition.items.GetByIndex(it.index)
73 it.record = &record{v.(*Item)}
74 it.index++
75 return true
76}