Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: datasource for gno.land/r/leon/hof integration with Gno.me #3247

Merged
merged 10 commits into from
Dec 16, 2024
64 changes: 64 additions & 0 deletions examples/gno.land/r/leon/hof/datasource.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package hof

import (
"errors"

"gno.land/p/demo/seqid"
"gno.land/p/demo/ufmt"
"gno.land/p/gnome/datasource"
jeronimoalbi marked this conversation as resolved.
Show resolved Hide resolved
)

type Datasource struct{}

func (ds Datasource) GetRecords(offset, _ int, _ string) datasource.RecordIter {
return &iterator{index: offset}
}

func (ds Datasource) GetContent(rawID string, fn func(content string)) error {
id, err := seqid.FromString(rawID)
if err != nil {
return err
}

v, ok := exhibition.itemsSorted.Get(id.String())
if !ok {
return errors.New("realm submission not found")
}

content := ufmt.Sprintf("# Submission #%d\n\n", int(id))
content += v.(*Item).Render(false)

fn(content)
return nil
}

type record struct {
item *Item
}

func (r record) ID() string { return r.item.id.String() }
func (r record) Title() string { return r.item.pkgpath }
func (r record) Tags() []string { return nil }

func (r record) Details() string {
return ufmt.Sprintf("Votes: ⏶ %d - ⏷ %d", r.item.upvote.Size(), r.item.downvote.Size())
}

type iterator struct {
index int
record *record
}

func (it iterator) Record() datasource.Record { return it.record }
func (it iterator) Err() error { return nil }

func (it *iterator) Next() bool {
if it.index >= exhibition.itemsSorted.Size() {
return false
}

_, v := exhibition.itemsSorted.GetByIndex(it.index)
it.record = &record{v.(*Item)}
it.index++
return true
}
Loading