values

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package values implements encoding objects into url.Values.

Example (EncodeSearch)
package main

import (
	"fmt"
	"net/url"

	"github.com/splunk/go-splunk-client/pkg/values"
)

type Action struct {
	// Name and Enabled are not added to url.Values by "normal" encoding.
	Name    string `values:"-"`
	Enabled bool   `values:"-"`

	// Parameters are added to url.Values by "normal" encoding (but with custom key calculation).
	Parameters map[string]string
}

func (a Action) GetURLKey(parentKey, childKey string) (string, error) {
	// the key for Action is <parentKey>.<a.Name>
	// this sets Parameters at <parentKey>.<a.Name>.<Parameter.key>
	return fmt.Sprint(parentKey, ".", a.Name), nil
}

type Actions []Action

// AddURLValues adds the list of enabled actions to the "actions" key.
func (actions Actions) AddURLValues(key string, v *url.Values) error {
	for _, action := range actions {
		if action.Enabled {
			v.Add("actions", action.Name)
		}
	}

	return nil
}

type Search struct {
	Name    string  `values:"name"`
	Actions Actions `values:"action"`
}

func main() {
	search := Search{
		Name: "my_search",
		Actions: Actions{
			{
				Name:    "email",
				Enabled: true,
				Parameters: map[string]string{
					"subject":   "Something happened!",
					"recipient": "joeuser@example.com",
				},
			},
		},
	}

	v, _ := values.Encode(search)
	fmt.Println(v.Encode())
}
Output:

action.email.recipient=joeuser%40example.com&action.email.subject=Something+happened%21&actions=email&name=my_search
Example (EncodeStanza)
package main

import (
	"fmt"

	"github.com/splunk/go-splunk-client/pkg/values"
)

type ConfStanza struct {
	File     string            `values:"-"`
	Stanza   string            `values:"-"`
	Disabled bool              `values:"disabled"`
	Values   map[string]string `values:",anonymize"`
}

func main() {
	myStanza := ConfStanza{
		File:   "myconffile",
		Stanza: "mystanza",
		Values: map[string]string{
			"key1": "value1",
		},
	}

	v, _ := values.Encode(myStanza)
	fmt.Println(v.Encode())
}
Output:

disabled=false&key1=value1

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(i interface{}) (url.Values, error)

Encode returns url.Values for a given input interface.

A struct field's key defaults to the name of the field. Nested values default to having their key as a dotted suffix to the parent object's key. An empty parent key results in the child key being used directly. The parent key for the original value passed to Encode is an empty string.

Anonymous struct fields also start with an empty parent key.

Struct tags can be used to customize the encoding behavior of specific fields. Tags are formatted as "<key>,<option>...". If <key> is left empty the default key value will be used, which is the name of the field. The field is not encoded if the full value of the tag is "-".

The name of the encoded value may also be determined by having it adhere to the values.KeyEncoder interface, of which the GetURLKey method accepts a parent and child key and returns a customized key.

Option flags can be included as comma-separated values after <key>. Supported options are:

• omitzero - Omit the field if it is the zero value for its type. Note that the zero value for a slice is nil. An empty slice is not treated as a nil slice.

• fillempty - Populate empty slices (including nil) with a single value. The assigned value will be the zero type for the slice element. If both fillempty and omitzero flags are given, omitzero has precedence. This permits empty and nil slices to behave differently, as configured.

• anonymize - Treat the field as if it were anonymous. This gives it an empty parent key.

An error is returned if a value's key is an empty string at any level of encoding.

Types

type URLKeyGetter

type URLKeyGetter interface {
	GetURLKey(parentKey string, childKeystring string) (string, error)
}

URLKeyGetter is the interface for types that implement custom key calculation prior to encoding to url.Values.

type URLValueGetter

type URLValueGetter interface {
	GetURLValue() interface{}
}

URLValueGetter is the interface for types that implement custom value calculation prior to encoding to url.Values.

type URLValuesAdder

type URLValuesAdder interface {
	AddURLValues(string, *url.Values) error
}

URLValuesAdder is the interface for types that implement custom encoding to url.Values for a given key in addition to default encoding. AddURLValues will be called in addition to the default encoding methods. It is not called if the encoded type is a URLValuesSetter or URLValueGetter.

type URLValuesSetter

type URLValuesSetter interface {
	SetURLValues(string, *url.Values) error
}

URLValuesSetter is the interface for types that implement custom encoding to url.Values for a given key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL