attributes

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: 7 Imported by: 0

Documentation

Overview

Package attributes provides types that can represent explicitly set values, including the zero values of their stored types, to enable HTTP requests to account for all intended parameter values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnmarshalJSONForNamedParametersCollections

func UnmarshalJSONForNamedParametersCollections(data []byte, dest interface{}) error

UnmarshalJSONForNamedParametersCollections unmarshals JSON data into the given dest interface. dest must be a pointer to a struct, and any struct fields with the "named_parameters_collection" tag must be of the NamedParametersCollection type.

This method exists to enable unmarshaling of the same level of a JSON document to a struct and also to NamedParametersCollection fields of the same struct.

func UnmarshalJSONForParameters

func UnmarshalJSONForParameters(data []byte, dest interface{}) error

UnmarshalJSONForParameters unmarshals JSON data into the given dest interface. dest must be a pointer to a struct, and any struct fields with the "parameters" tag must be of the Parameters type.

This method exists to enable unmarshaling of the same level of a JSON document to a struct and also to Parameters fields of the same struct.

Types

type Explicit

type Explicit[T Storable] struct {
	// contains filtered or unexported fields
}

Explicit is permits storing a value such that it can be explicitly empty/zero.

Example (Bool)
package main

import (
	"fmt"

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

func main() {
	type knowledgeObject struct {
		Name     string                    `values:"name"`
		Disabled attributes.Explicit[bool] `values:"disabled,omitzero"`
	}

	myObject := knowledgeObject{
		Name: "my_knowledge_object",
	}
	// myObjectURLValues will not have a value for Disabled as it has not been set
	myObjectURLValues, _ := values.Encode(myObject)
	fmt.Printf("myObjectURLValues without explicitly set Disabled: %s\n", myObjectURLValues)

	myObject.Disabled = attributes.NewExplicit(false)
	// myObjectURLValues will have a value of false for Disabled as it has been set
	myObjectURLValues, _ = values.Encode(myObject)
	fmt.Printf("myObjectURLValues with explicitly set Disabled: %s\n", myObjectURLValues)
}
Output:

myObjectURLValues without explicitly set Disabled: map[name:[my_knowledge_object]]
myObjectURLValues with explicitly set Disabled: map[disabled:[false] name:[my_knowledge_object]]

func NewExplicit

func NewExplicit[T Storable](value T) (newExplicitValue Explicit[T])

NewExplicit returns a new Explicit with its value explicitly set.

func (Explicit[T]) Bool

func (e Explicit[T]) Bool() (value bool, ok bool)

Bool returns a value indicating the boolean representation of the stored value, and another boolean that will be true only if the value was explicitly set, and it can be parsed by strconv.ParseBool without error.

func (Explicit[T]) GetURLValue

func (e Explicit[T]) GetURLValue() interface{}

GetURLValue implements custom encoding of its url.Values value.

func (*Explicit[T]) Set

func (e *Explicit[T]) Set(value T)

Set explicitly sets the value.

func (Explicit[T]) String

func (e Explicit[T]) String() string

String returns the defualt string representation of the stored value.

func (*Explicit[T]) UnmarshalJSON

func (e *Explicit[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling. The unmarshaled is explicitly set.

func (Explicit[T]) Value

func (e Explicit[T]) Value() T

Value returns the stored value.

func (Explicit[T]) ValueOk

func (e Explicit[T]) ValueOk() (T, bool)

ValueOk returns the stored value and a boolean indicating if it was explicitly set.

type NamedParameters

type NamedParameters struct {
	// Name is the overall name of this set of Parameters. It is likely the leftmost segment
	// of a dotted parameter name, such as "actions" for "actions.email".
	Name string `values:"-"`

	// Status is the string representation of a NamedParameters' status. This is typically
	// true/false or 0/1, and is the value associated directly with the name segment, such as
	// email=true.
	Status Explicit[string] `values:",omitzero,anonymize"`

	Parameters Parameters `values:",omitzero,anonymize"`
}

NamedParameters represent a set of Parameters that are associated with an overall Name.

func (NamedParameters) GetURLKey

func (params NamedParameters) GetURLKey(parentKey string, childKey string) (string, error)

GetURLKey implements custom key encoding for url.Values.

type NamedParametersCollection

type NamedParametersCollection []NamedParameters

NamedParametersCollection is a collection of NamedParameters.

func (NamedParametersCollection) EnabledNames

func (collection NamedParametersCollection) EnabledNames() []string

EnabledNames returns a list of Names of the member NamedParameters that have a true Status value.

type Parameters

type Parameters map[string]string

Parameters is a map of parameter names to string values.

func (Parameters) SetURLValues

func (p Parameters) SetURLValues(key string, v *url.Values) error

SetURLValues implements custom encoding into url.Values.

func (*Parameters) UnmarshalJSON

func (p *Parameters) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling which assumes the content being unmarshaled is a simple map of strings to a single value (string, bool, float, int). It returns an error if a value other than these types is encountered.

type Storable

type Storable interface {
	bool | int | string
}

Storable defines the types storable by ExplicitValue.

Jump to

Keyboard shortcuts

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