~samwhited/xmpp

52be89df867be9fc4e8ba83b0c3880887a6485ba — Sam Whited 4 years ago 2488529
form: Rename Option and FieldOption types

Renamed to "Field" and "Option" respectively.
3 files changed, 28 insertions(+), 28 deletions(-)

M form/fields.go
M form/form.go
M form/options.go
M form/fields.go => form/fields.go +12 -12
@@ 17,7 17,7 @@ type field struct {
	Desc     string     `xml:"desc,omitempty"`
	Value    []string   `xml:"value,omitempty"`
	Required *struct{}  `xml:"required,omitempty"`
	Option   []fieldopt `xml:"option,omitempty"`
	Field    []fieldopt `xml:"option,omitempty"`
}

type fieldopt struct {


@@ 25,7 25,7 @@ type fieldopt struct {
	Value   string   `xml:"value,omitempty"`
}

func newField(typ, varName string, o ...FieldOption) func(data *Data) {
func newField(typ, varName string, o ...Option) func(data *Data) {
	return func(data *Data) {
		f := field{
			Typ: typ,


@@ 38,56 38,56 @@ func newField(typ, varName string, o ...FieldOption) func(data *Data) {

// Boolean fields enable an entity to gather or provide an either-or choice
// between two options.
func Boolean(varName string, o ...FieldOption) Option {
func Boolean(varName string, o ...Option) Field {
	return newField("boolean", "", o...)
}

// Fixed is intended for data description (e.g., human-readable text such as
// "section" headers) rather than data gathering or provision.
func Fixed(o ...FieldOption) Option {
func Fixed(o ...Option) Field {
	return newField("fixed", "", o...)
}

// Hidden fields are not shown by the form-submitting entity, but instead are
// returned, generally unmodified, with the form.
func Hidden(varName string, o ...FieldOption) Option {
func Hidden(varName string, o ...Option) Field {
	return newField("hidden", varName, o...)
}

// JIDMulti enables an entity to gather or provide multiple Jabber IDs.
func JIDMulti(varName string, o ...FieldOption) Option {
func JIDMulti(varName string, o ...Option) Field {
	return newField("jid-multi", varName, o...)
}

// JID enables an entity to gather or provide a Jabber ID.
func JID(varName string, o ...FieldOption) Option {
func JID(varName string, o ...Option) Field {
	return newField("jid-single", varName, o...)
}

// ListMulti enables an entity to gather or provide one or more entries from a
// list.
func ListMulti(varName string, o ...FieldOption) Option {
func ListMulti(varName string, o ...Option) Field {
	return newField("list-multi", varName, o...)
}

// ListSingle enables an entity to gather or provide a single entry from a list.
func ListSingle(varName string, o ...FieldOption) Option {
func ListSingle(varName string, o ...Option) Field {
	return newField("list-single", varName, o...)
}

// TextMulti enables an entity to gather or provide multiple lines of text.
func TextMulti(varName string, o ...FieldOption) Option {
func TextMulti(varName string, o ...Option) Field {
	return newField("text-multi", varName, o...)
}

// TextPrivate enables an entity to gather or provide a line of text that should
// be obscured in the submitting entities interface (eg. with multiple
// asterisks).
func TextPrivate(varName string, o ...FieldOption) Option {
func TextPrivate(varName string, o ...Option) Field {
	return newField("text-private", varName, o...)
}

// TextSingle enables an entity to gather or provide a line of text.
func TextSingle(varName string, o ...FieldOption) Option {
func TextSingle(varName string, o ...Option) Field {
	return newField("text-single", varName, o...)
}

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

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

M form/options.go => form/options.go +15 -15
@@ 4,11 4,11 @@

package form

// An Option is used to define the behavior and appearance of a data form.
type Option func(*Data)
// An Field is used to define the behavior and appearance of a data form.
type Field func(*Data)

// Title sets a form's title.
func Title(s string) Option {
func Title(s string) Field {
	return func(data *Data) {
		data.title.Text = s
	}


@@ 16,30 16,30 @@ func Title(s string) Option {

// Instructions adds new textual instructions to the form.
// Multiple uses of the option result in multiple sets of instructions.
func Instructions(s string) Option {
func Instructions(s string) Field {
	return func(data *Data) {
		data.children = append(data.children, instructions{Text: s})
	}
}

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

// A FieldOption is used to define the behavior and appearance of a form field.
type FieldOption func(*field)
// A Option is used to define the behavior and appearance of a form field.
type Option func(*field)

var (
	// Required flags the field as required in order for the form to be considered
	// valid.
	Required FieldOption = required
	Required Option = required
)

var (
	required FieldOption = func(f *field) {
	required Option = func(f *field) {
		f.Required = &struct{}{}
	}
)


@@ 50,7 50,7 @@ var (
// Desc should not contain newlines (the \n and \r characters), since layout is
// the responsibility of a user agent.
// However, it does nothing to prevent them from being added.
func Desc(s string) FieldOption {
func Desc(s string) Option {
	return func(f *field) {
		f.Desc = s
	}


@@ 62,23 62,23 @@ func Desc(s string) FieldOption {
// data form of type "result".
// Fields of type ListMulti, JidMulti, TextMulti, and Hidden may contain more
// than one Value; all other field types will only use the first Value.
func Value(s string) FieldOption {
func Value(s string) Option {
	return func(f *field) {
		f.Value = append(f.Value, s)
	}
}

// ListOption is one of the values in a list.
// ListField is one of the values in a list.
// It has no effect on any non-list field type.
func ListOption(s string) FieldOption {
func ListField(s string) Option {
	return func(f *field) {
		f.Option = append(f.Option, fieldopt{
		f.Field = append(f.Field, fieldopt{
			Value: s,
		})
	}
}

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