Documentation
¶
Overview ¶
Package selective provides functionality to translate one struct type into another by selectively including fields with matching field tags.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
Encode returns a value for an input struct value calculated by:
• If no fields non-empty "selective" tag values, the input is returned directly, retaining unexported fields and methods.
• Any field with a non-empty "selective" tag value is retained only if its tag contains the tag provided to Encode.
• Struct field values are calculated by calling this function on the input field's value with the same tag.
Returned values with changes (structs that contain at least one field with a non-empty "selective" tag) will lose unexported fields and methods. This is due to reflection being unable to retain them.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/splunk/go-splunk-client/pkg/selective" ) type Discography struct { Albums []string `json:"albums" selective:"albums"` Singles []string `json:"singles" selective:"singles"` } type Artist struct { // Name defines no selective tag, so it is always selected Name string `json:"name"` // Discography is selected for both albums and singles Discography Discography `json:"discography" selective:"albums,singles"` // Members is selected only for personnel Members []string `json:"members" selective:"personnel"` } func main() { artist := Artist{ Name: "The Refreshments", Discography: Discography{ Albums: []string{ "Fizzy Fuzzy Big and Buzzy", "The Bottle and Fresh Horses", }, Singles: []string{ "Yahoos and Triangles", }, }, Members: []string{ "Roger Clyne", "Dusty Denham", "P.H. Naffah", "Buddy Edwards", "Brian David Blush", }, } artistAlbumsOnly, _ := selective.Encode(artist, "albums") artistAlbumsOnlyJSON, _ := json.MarshalIndent(artistAlbumsOnly, "", " ") fmt.Printf("%s", artistAlbumsOnlyJSON) }
Output: { "name": "The Refreshments", "discography": { "albums": [ "Fizzy Fuzzy Big and Buzzy", "The Bottle and Fresh Horses" ] } }
Types ¶
This section is empty.