~samwhited/xmpp

00fbb05d177e9913368849ce22638bb073ddd921 — Sam Whited 3 years ago 6ab0fec
XEP-0004: Data Forms support
4 files changed, 40 insertions(+), 7 deletions(-)

A form/apitest.go
M form/fields.go
M form/form.go
M form/options.go
A form/apitest.go => form/apitest.go +33 -0
@@ 0,0 1,33 @@
// +build ignore

package main

import (
	"bytes"
	"encoding/xml"
	"fmt"
	"log"

	"mellium.im/xmpp/form"
)

func main() {
	b := new(bytes.Buffer)
	e := xml.NewEncoder(b)
	e.Indent("", "\t")
	f := form.New(
		form.Title("Title"),
		form.Instructions("Instructions to fill out the form!"),
		form.Boolean("bool", form.Required),
		form.Fixed(),
	)
	if err := e.Encode(f); err != nil {
		log.Fatal(err)
	}
	fmt.Println(b.String())

	d := xml.NewDecoder(conn)

	data := form.Data{}
	xml.Decode(&data)
}

M form/fields.go => form/fields.go +1 -1
@@ 31,7 31,7 @@ func newField(typ, id string, o ...Option) func(data *Data) {
			Typ: typ,
			Var: id,
		}
		getFieldOpts(&f, o...)
		getOpts(&f, o...)
		data.children = append(data.children, f)
	}
}

M form/form.go => form/form.go +2 -2
@@ 62,8 62,8 @@ type instructions struct {
}

// New builds a new data form from the provided options.
func New(o ...Field) *Data {
func New(f ...Field) *Data {
	form := &Data{typ: "form"}
	getOpts(form, o...)
	getFields(form, f...)
	return form
}

M form/options.go => form/options.go +4 -4
@@ 22,11 22,11 @@ func Instructions(s string) Field {
	}
}

func getOpts(data *Data, o ...Field) {
	for _, f := range o {
func getFields(data *Data, fields ...Field) error {
	for _, f := range fields {
		f(data)
	}
	return
	return nil
}

// A Option is used to define the behavior and appearance of a form field.


@@ 78,7 78,7 @@ func ListField(s string) Option {
	}
}

func getFieldOpts(f *field, o ...Option) {
func getOpts(f *field, o ...Option) {
	for _, opt := range o {
		opt(f)
	}