mapstriface

package
Version: v6.1.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mapstriface contains utilities for transforming map[string]interface{} objects into metricbeat events. For example, given this input object:

input := map[string]interface{}{
	"testString":       "hello",
	"testInt":          42,
	"testIntFromFloat": 42.0,
	"testIntFromInt64": int64(42),
	"testBool":         true,
	"testObj": map[string]interface{}{
		"testObjString": "hello, object",
	},
	"testNonNestedObj": "hello from top level",
}

And the requirement to transform it into this one:

common.MapStr{
	"test_string":         "hello",
	"test_int":            int64(42),
	"test_int_from_float": int64(42),
	"test_int_from_int64": int64(42),
	"test_bool":           true,
	"test_time":           common.Time(ts),
	"test_obj_1": common.MapStr{
		"test": "hello from top level",
	},
	"test_obj_2": common.MapStr{
		"test": "hello, object",
	},
}

It can be done with the following code:

schema := s.Schema{
	"test_string":         Str("testString"),
	"test_int":            Int("testInt"),
	"test_int_from_float": Int("testIntFromFloat"),
	"test_int_from_int64": Int("testIntFromInt64"),
	"test_bool":           Bool("testBool"),
	"test_time":           Time("testTime"),
	"test_obj_1": s.Object{
		"test": Str("testNonNestedObj"),
	},
	"test_obj_2": Dict("testObj", s.Schema{
		"test": Str("testObjString"),
	}),
}
output := schema.Apply(input)

Note that this allows for converting, renaming, and restructuring the data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(key string, opts ...schema.SchemaOption) schema.Conv

Bool creates a Conv object for converting booleans.

func Ifc

func Ifc(key string, opts ...schema.SchemaOption) schema.Conv

Ifc creates a schema.Conv object for converting the given data to interface.

func Int

func Int(key string, opts ...schema.SchemaOption) schema.Conv

Int creates a Conv object for converting integers. Acceptable input types are int64, int, and float64.

func Str

func Str(key string, opts ...schema.SchemaOption) schema.Conv

Str creates a schema.Conv object for converting strings.

func StrFromNum

func StrFromNum(key string, opts ...schema.SchemaOption) schema.Conv

StrFromNum creates a schema.Conv object that transforms numbers to strings.

func Time

func Time(key string, opts ...schema.SchemaOption) schema.Conv

Time creates a Conv object for converting Time objects.

Types

type ConvMap

type ConvMap struct {
	Key      string        // The key in the data map
	Schema   schema.Schema // The schema describing how to convert the sub-map
	Optional bool
}

func Dict

func Dict(key string, s schema.Schema, opts ...DictSchemaOption) ConvMap

func DictOptional

func DictOptional(c ConvMap) ConvMap

The optional flag suppresses the error message in case the key doesn't exist or results in an error.

func (ConvMap) HasKey

func (convMap ConvMap) HasKey(key string) bool

func (ConvMap) Map

func (convMap ConvMap) Map(key string, event common.MapStr, data map[string]interface{}) *schema.Errors

Map drills down in the data dictionary by using the key

type DictSchemaOption

type DictSchemaOption func(c ConvMap) ConvMap

SchemaOption is for adding optional parameters to the conversion functions

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL