extmap

package module
v2.0.0-...-f24207c Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package structs contains various utilities functions to work with structs.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultTagName is the default tag name for struct fields which provides
	// a more granular to tweak certain structs. Lookup the necessary functions
	// for more info.
	DefaultTagName = "structs" // struct's field default tag name
)
View Source
var ErrNilMap = errors.New("nil map")

ErrNilMap ...

View Source
var ErrUnsupportedType = errors.New("error unsupported type")

Functions

func FillMap

func FillMap(s interface{}, out map[string]interface{})

FillMap is the same as Map. Instead of returning the output, it fills the given map.

func GoMap

func GoMap(s interface{}) map[string]interface{}

GoMap converts the given struct to a map[string]interface{}. For more info refer to structData types Map() method. It panics if s's kind is not struct.

func HasZero

func HasZero(s interface{}) bool

HasZero returns true if any field is equal to a zero value. For more info refer to structData types HasZero() method. It panics if s's kind is not struct.

func IsStruct

func IsStruct(s interface{}) bool

IsStruct returns true if the given variable is a struct or a pointer to struct.

func IsZero

func IsZero(s interface{}) bool

IsZero returns true if all fields is equal to a zero value. For more info refer to structData types IsZero() method. It panics if s's kind is not struct.

func Merge

func Merge(sources ...map[string]any) map[string]any

func Name

func Name(s interface{}) string

Name returns the structs's type name within its package. It returns an empty string for unnamed types. It panics if s's kind is not struct.

func Names

func Names(s interface{}) []string

Names returns a slice of field names. For more info refer to structData types Names() method. It panics if s's kind is not struct.

func ParseInt

func ParseInt(v interface{}) (int64, bool)

ParseInt parse interface to int64

func ParseInt64

func ParseInt64(v interface{}) (int64, bool)

func ParseNumber

func ParseNumber(v interface{}) (float64, bool)

ParseNumber parse interface to number

func ParseString

func ParseString(v interface{}) (string, bool)

ParseString parse interface to string

func Values

func Values(s interface{}) []interface{}

Values converts the given struct to a []interface{}. For more info refer to structData types Values() method. It panics if s's kind is not struct.

Types

type Field

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

Field represents a single struct field that encapsulates high level functions around the field.

func Fields

func Fields(s interface{}) []*Field

Fields returns a slice of *Field. For more info refer to structData types Fields() method. It panics if s's kind is not struct.

func (*Field) Field

func (f *Field) Field(name string) *Field

Field returns the field from a nested struct. It panics if the nested struct is not exported or if the field was not found.

func (*Field) FieldOk

func (f *Field) FieldOk(name string) (*Field, bool)

FieldOk returns the field from a nested struct. The boolean returns whether the field was found (true) or not (false).

func (*Field) Fields

func (f *Field) Fields() []*Field

Fields returns a slice of Fields. This is particular handy to get the fields of a nested struct . A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field *http.Request `structs:"-"`

It panics if field is not exported or if field's kind is not struct

func (*Field) IsEmbedded

func (f *Field) IsEmbedded() bool

IsEmbedded returns true if the given field is an anonymous field (embedded)

func (*Field) IsExported

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported.

func (*Field) IsZero

func (f *Field) IsZero() bool

IsZero returns true if the given field is not initialized (has a zero value). It panics if the field is not exported.

func (*Field) Kind

func (f *Field) Kind() reflect.Kind

Kind returns the fields kind, such as "string", "map", "bool", etc ..

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the given field

func (*Field) Set

func (f *Field) Set(val interface{}) error

Set sets the field to given value v. It returns an error if the field is not settable (not addressable or not exported) or if the given value's type doesn't match the fields type.

func (*Field) Tag

func (f *Field) Tag(key string) string

Tag returns the value associated with key in the tag string. If there is no such key in the tag, Tag returns the empty string.

func (*Field) Value

func (f *Field) Value() interface{}

Value returns the underlying value of the field. It panics if the field is not exported.

func (*Field) Zero

func (f *Field) Zero() error

Zero sets the field to its zero value. It returns an error if the field is not settable (not addressable or not exported).

type JSONer

type JSONer interface {
	ParseJson([]byte) error
	ToJSON() (v []byte, err error)
}

JSONer ...

type Map

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

func New

func New(ss ...SettingOption) *Map

New create a map interface

func ToMap

func ToMap(p any) *Map

ToMap transfer to map[comparable]any to Map

func (*Map) Append

func (m *Map) Append(source *Map) *Map

Append append source map to target map; if the key value is exist and it is a []interface value, this will append into it otherwise, it will replace the value

func (*Map) Check

func (m *Map) Check(s ...string) int

Check check all input keys return -1 if all is exist return index when not found

func (*Map) Clone

func (m *Map) Clone() Map

Clone copy a map

func (*Map) Delete

func (m *Map) Delete(key string) bool

Delete delete key value if key is exist

func (*Map) DeletePath

func (m *Map) DeletePath(keys []string) bool

DeletePath delete keys value if keys is exist

func (*Map) Expect

func (m *Map) Expect(keys []string) Map

Expect get map expect keys

func (Map) Get

func (m Map) Get(key string) any

Get get interface from map with out default

func (*Map) GetArray

func (m *Map) GetArray(s string) []any

GetArray get []interface value with out default

func (*Map) GetArrayD

func (m *Map) GetArrayD(s string, d []any) []any

GetArrayD get []interface value with default

func (*Map) GetBool

func (m *Map) GetBool(s string) bool

GetBool get bool from map with out default

func (*Map) GetBoolD

func (m *Map) GetBoolD(s string, b bool) bool

GetBoolD get bool from map with default

func (*Map) GetBytes

func (m *Map) GetBytes(s string) []byte

GetBytes get bytes from map with default

func (*Map) GetBytesD

func (m *Map) GetBytesD(s string, d []byte) []byte

GetBytesD get bytes from map with default

func (*Map) GetD

func (m *Map) GetD(s string, d any) any

GetD get interface from map with default

func (*Map) GetInt64

func (m *Map) GetInt64(s string) (int64, bool)

GetInt64 get int64 from map with out default

func (*Map) GetInt64D

func (m *Map) GetInt64D(s string, d int64) int64

GetInt64D get int64 from map with default

func (*Map) GetMap

func (m *Map) GetMap(s string) *Map

GetMap get map from map with out default

func (*Map) GetMapArray

func (m *Map) GetMapArray(s string) []*Map

GetMapArray get map from map with out default

func (*Map) GetMapArrayD

func (m *Map) GetMapArrayD(s string, d []*Map) []*Map

GetMapArrayD get map from map with default

func (*Map) GetMapD

func (m *Map) GetMapD(s string, d *Map) *Map

GetMapD get map from map with default

func (*Map) GetNumber

func (m *Map) GetNumber(s string) (float64, bool)

GetNumber get float64 from map with out default

func (*Map) GetNumberD

func (m *Map) GetNumberD(s string, d float64) float64

GetNumberD get float64 from map with default

func (*Map) GetPath

func (m *Map) GetPath(keys []string) any

GetPath returns the element in the tree indicated by 'keys'. If keys is of length zero, the current tree is returned.

func (*Map) GetString

func (m *Map) GetString(s string) string

GetString get string from map with out default

func (*Map) GetStringArray

func (m *Map) GetStringArray(s string) []string

GetStringArray get string from map with out default

func (*Map) GetStringArrayD

func (m *Map) GetStringArrayD(s string, d []string) []string

GetStringD get string from map with default

func (*Map) GetStringD

func (m *Map) GetStringD(s string, d string) string

GetStringD get string from map with default

func (*Map) GoMap

func (m *Map) GoMap() map[string]any

GoMap trans return a map[string]interface from Map

func (*Map) Has

func (m *Map) Has(key string) bool

Has check if key exist

func (*Map) HasPath

func (m *Map) HasPath(keys []string) bool

HasPath returns true if the given path of keys exists, false otherwise.

func (*Map) Join

func (m *Map) Join(source *Map) *Map

Join insert map s to m with out replace

func (*Map) Map

func (m *Map) Map() *Map

Map implements MapAble by self

func (Map) MarshalJSON

func (m Map) MarshalJSON() ([]byte, error)

func (*Map) Merge

func (m *Map) Merge(maps ...*Map) *Map

Merge marge all maps to target Map, the newer value will replace the older value

func (*Map) Only

func (m *Map) Only(keys []string) *Map

Only get map with keys

func (*Map) ParseJSON

func (m *Map) ParseJSON(b []byte) error

ParseJSON parse JSON bytes to map

func (*Map) Query

func (m *Map) Query(key string) (any, error)

func (*Map) Range

func (m *Map) Range(f func(key string, value any) bool)

Range range all maps

func (*Map) Replace

func (m *Map) Replace(s string, v any) *Map

Replace replace will set value, if the key is exist

func (*Map) ReplaceFromMap

func (m *Map) ReplaceFromMap(key string, v *Map) *Map

ReplaceFromMap replace will set value from other map, if the key is exist from the both map

func (*Map) ReplaceJoin

func (m *Map) ReplaceJoin(source *Map) *Map

ReplaceJoin insert map s to m with replace

func (*Map) Set

func (m *Map) Set(key string, v any) *Map

Set is a set interface

func (*Map) SetNil

func (m *Map) SetNil(key string, val any) *Map

SetNil set value, if the key is not exist

func (*Map) SetPath

func (m *Map) SetPath(keys []string, v any) *Map

SetPath is the same as SetPath, but allows you to provide comment information to the key, that will be reused by Marshal().

func (*Map) SortKeys

func (m *Map) SortKeys() []string

SortKeys 排列key

func (Map) String

func (m Map) String() string

func (*Map) ToEncodeURL

func (m *Map) ToEncodeURL() string

ToEncodeURL transfer map to url encode

func (*Map) ToJSON

func (m *Map) ToJSON() (v []byte, err error)

ToJSON transfer map to JSON

func (Map) UnmarshalJSON

func (m Map) UnmarshalJSON(bytes []byte) error

type Setting

type Setting struct {
	Split bool
}

type SettingOption

type SettingOption func(op *Setting)

type String

type String string

String String

func ToString

func ToString(s string) String

ToString ToString

func (String) String

func (s String) String() string

String String

type Stringer

type Stringer interface {
	String() string
}

Stringer ...

type Struct

type Struct interface {
}

type XMLer

type XMLer interface {
	ParseXML([]byte) error
	ToXML() []byte
}

XMLer ...

Jump to

Keyboard shortcuts

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