mapcast

package module
v0.0.0-...-76b07c9 Latest Latest
Warning

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

Go to latest
Published: May 27, 2016 License: MIT Imports: 6 Imported by: 0

README

mapcast

Convert map[string]string into map[string]interface using a reference struct to sniff types. Optionally can expect the struct's json tag names. Optionally can returned data to bson tag name.

WHY?

In order to use query string values as values in mongo db calls. That's my use case.

Cast

Casts map[string]string referencing any struct to map[string]interface

CastViaJson

Casts map[string]string referencing any struct to map[string]interface expecting json name in input

Respects the "-" flag.

CastViaJsonToBson

Casts map[string]string referencing any struct to map[string]interface expecting json name in input and returning bson field names in map

CastViaProtobufToBson

Casts map[string]string referencing any struct to map[string]interface expecting protobuf name in input and returning bson field names in map

Examples

type myStruct struct {
    Field int `json:"input_name" bson:"output_name"`
    Hidden float32 `json:"-" bson:"hidden_field"`
}

myInput := map[string]string{"input_name": "345"}

Cast(myInput, myStruct{}) 
// returns map{"Field" : 345}

CastViaJson(myInput, myStruct{}) 
// returns map{"input_name" : 345}

CastViaJsonToBson(myInput, myStruct{}) 
// returns map{"output_name" : 345}

myMultiInput := map[string][]string{"input_name" : []string{"345}}

CastMultiViaJsonToBson(myMultiInput, myStruct{}) 
// returns map{"output_name" : []int[345]}
Supported Types

It can convert to these types so far (againg ObjectId is here since my use case is mongo related)

  • bool
  • string
  • int
  • int8
  • int16
  • int32
  • uint
  • uint8
  • uint16
  • uint32
  • float64
  • float128
  • bson.ObjectId

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BsonFieldNamer

func BsonFieldNamer(field reflect.StructField) string

func Cast

func Cast(inMap map[string]string, target interface{}) (outMap map[string]interface{})

func CastMultiple

func CastMultiple(inMap map[string][]string, target interface{}) (outMap map[string][]interface{})

func CastMultipleViaJson

func CastMultipleViaJson(inMap map[string][]string, target interface{}) (outMap map[string][]interface{})

func CastMultipleViaJsonToBson

func CastMultipleViaJsonToBson(inMap map[string][]string, target interface{}) (outMap map[string][]interface{})

func CastMultipleViaProtoToBson

func CastMultipleViaProtoToBson(inMap map[string][]string, target interface{}) (outMap map[string][]interface{})

func CastViaJson

func CastViaJson(inMap map[string]string, target interface{}) (outMap map[string]interface{})

func CastViaJsonToBson

func CastViaJsonToBson(inMap map[string]string, target interface{}) (outMap map[string]interface{})

func CastViaProtoToBson

func CastViaProtoToBson(inMap map[string]string, target interface{}) (outMap map[string]interface{})

func DBFieldNamer

func DBFieldNamer(field reflect.StructField) string

func JsonFieldNamer

func JsonFieldNamer(field reflect.StructField) string

func ProtoFieldNamer

func ProtoFieldNamer(field reflect.StructField) string

func StdFieldNamer

func StdFieldNamer(field reflect.StructField) string

Types

type MapCaster

type MapCaster struct {
	// contains filtered or unexported fields
}

func NewMapCaster

func NewMapCaster() *MapCaster

func (*MapCaster) Cast

func (m *MapCaster) Cast(inMap map[string]string, target interface{}) map[string]interface{}

func (*MapCaster) CastArrayValue

func (m *MapCaster) CastArrayValue(inMap map[string][]string, target interface{}) map[string][]interface{}

func (*MapCaster) Input

func (m *MapCaster) Input(namer fieldNamer)

func (*MapCaster) Output

func (m *MapCaster) Output(namer fieldNamer)

func (*MapCaster) TimeFormat

func (m *MapCaster) TimeFormat(format string)

Jump to

Keyboard shortcuts

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