Skip to content

Commit

Permalink
add Bytes and Slice (array alias) functions
Browse files Browse the repository at this point in the history
  • Loading branch information
arnecls committed Aug 13, 2018
1 parent 502a33e commit 2651625
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion tcontainer/marshalmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,25 @@ func (mmap MarshalMap) String(key string) (string, error) {
return strValue, nil
}

// Bytes returns a value at key that is expected to be a []byte
func (mmap MarshalMap) Bytes(key string) ([]byte, error) {
val, exists := mmap.Value(key)
if !exists {
return []byte{}, fmt.Errorf(`"%s" is not set`, key)
}

bytesValue, isBytes := val.([]byte)
if !isBytes {
return []byte{}, fmt.Errorf(`"%s" is expected to be a []byte`, key)
}
return bytesValue, nil
}

// Slice is an alias for Array
func (mmap MarshalMap) Slice(key string) ([]interface{}, error) {
return mmap.Array(key)
}

// Array returns a value at key that is expected to be a []interface{}
func (mmap MarshalMap) Array(key string) ([]interface{}, error) {
val, exists := mmap.Value(key)
Expand Down Expand Up @@ -276,6 +295,11 @@ func castToStringArray(key string, value interface{}) ([]string, error) {
}
}

// StringSlice is an alias for StringArray
func (mmap MarshalMap) StringSlice(key string) ([]string, error) {
return mmap.StringArray(key)
}

// StringArray returns a value at key that is expected to be a []string
// This function supports conversion (by copy) from
// * []interface{}
Expand Down Expand Up @@ -314,7 +338,12 @@ func castToInt64Array(key string, value interface{}) ([]int64, error) {
}
}

// IntArray returns a value at key that is expected to be a []int64
// Int64Slice is an alias for Int64Array
func (mmap MarshalMap) Int64Slice(key string) ([]int64, error) {
return mmap.Int64Array(key)
}

// Int64Array returns a value at key that is expected to be a []int64
// This function supports conversion (by copy) from
// * []interface{}
func (mmap MarshalMap) Int64Array(key string) ([]int64, error) {
Expand Down Expand Up @@ -366,6 +395,11 @@ func (mmap MarshalMap) StringMap(key string) (map[string]string, error) {
}
}

// StringSliceMap is an alias for StringArrayMap
func (mmap MarshalMap) StringSliceMap(key string) (map[string][]string, error) {
return mmap.StringArrayMap(key)
}

// StringArrayMap returns a value at key that is expected to be a
// map[string][]string. This function supports conversion (by copy) from
// * map[interface{}][]interface{}
Expand Down

0 comments on commit 2651625

Please sign in to comment.