Skip to content

Commit

Permalink
feat(examples): gno-forms json methods
Browse files Browse the repository at this point in the history
  • Loading branch information
agherasie committed Jan 16, 2025
1 parent 30dbb07 commit 74179f5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
52 changes: 52 additions & 0 deletions examples/gno.land/p/demo/forms/forms_json.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package forms

import (
"std"
"strings"

"gno.land/p/demo/json"
"gno.land/p/demo/ufmt"
)

func (b *json.NodeBuilder) WriteFormFields(key string, fields []Field) *json.NodeBuilder {
b.WriteArray(key, func(builder *json.ArrayBuilder) {
for _, field := range fields {
builder.WriteObject(func(builder *json.NodeBuilder) {
builder.WriteString("label", field.Label).
WriteString("fieldType", field.FieldType).
WriteBool("required", field.Required)
})
}
})
return b
}

func (b *json.NodeBuilder) WriteFormSubmission(key string, submission *Submission) *json.NodeBuilder {
b.WriteObject(key, func(builder *json.NodeBuilder) {
builder.WriteString("submittedAt", submission.SubmittedAt.Format("2006-01-02 15:04:05")).
WriteString("answers", strings.ReplaceAll(submission.Answers, "\"", "'"))
})
return b
}

func (b *json.NodeBuilder) WriteForm(key string, value *Form) *json.NodeBuilder {
b.WriteString("id", value.ID).
WriteString("owner", value.Owner.String()).
WriteString("title", value.Title).
WriteString("description", value.Description).
WriteString("createdAt", value.CreatedAt.Format("2006-01-02 15:04:05")).
WriteFormFields("fields", value.Fields)
return b
}

func (b *json.ArrayBuilder) WriteForm(key string, value *Form) *json.ArrayBuilder {
b.WriteObject(func(builder *json.NodeBuilder) {
builder.WriteString("id", value.ID).
WriteString("owner", value.Owner.String()).
WriteString("title", value.Title).
WriteString("description", value.Description).
WriteString("createdAt", value.CreatedAt.Format("2006-01-02 15:04:05")).
WriteFormFields("fields", value.Fields)
})
return b
}
32 changes: 3 additions & 29 deletions examples/gno.land/r/demo/forms/forms.gno
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,7 @@ func GetForms() string {
builder.WriteArray("forms", func(builder *json.ArrayBuilder) {
for _, form := range db.Forms {
builder.WriteObject(func(builder *json.NodeBuilder) {
builder.WriteString("id", form.ID).
WriteString("owner", form.Owner.String()).
WriteString("title", form.Title).
WriteString("description", form.Description).
WriteString("createdAt", form.CreatedAt.Format("2006-01-02 15:04:05")).
WriteArray("fields", func(builder *json.ArrayBuilder) {
for _, field := range form.Fields {
builder.WriteObject(func(builder *json.NodeBuilder) {
builder.WriteString("label", field.Label).
WriteString("fieldType", field.FieldType).
WriteBool("required", field.Required)
})
}
})
builder.WriteForm("form", form)
})
}
})
Expand All @@ -63,24 +50,11 @@ func GetFormByID(id string) string {

builder := json.Builder()

builder.WriteString("id", form.ID).
WriteString("owner", form.Owner.String()).
WriteString("title", form.Title).
WriteString("description", form.Description).
WriteString("createdAt", form.CreatedAt.Format("2006-01-02 15:04:05")).
WriteArray("fields", func(builder *json.ArrayBuilder) {
for _, field := range form.Fields {
builder.WriteObject(func(builder *json.NodeBuilder) {
builder.WriteString("label", field.Label).WriteString("fieldType", field.FieldType).WriteBool("required", field.Required)
})
}
}).
builder.WriteForm("form", form).
WriteObject("submissions", func(builder *json.NodeBuilder) {
formSubmissions := db.GetSubmissionsByFormID(form.ID)
for _, submission := range formSubmissions {
builder.WriteObject(submission.Author.String(), func(builder *json.NodeBuilder) {
builder.WriteString("submittedAt", submission.SubmittedAt.Format("2006-01-02 15:04:05")).WriteString("answers", strings.ReplaceAll(submission.Answers, "\"", "'"))
})
builder.WriteFormSubmission(submission.Author.String(), submission)
}
})

Expand Down

0 comments on commit 74179f5

Please sign in to comment.