gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/stretchr/objx/type_specific.go (about)

     1  package objx
     2  
     3  /*
     4     MSI (map[string]interface{} and []map[string]interface{})
     5  */
     6  
     7  // MSI gets the value as a map[string]interface{}, returns the optionalDefault
     8  // value or a system default object if the value is the wrong type.
     9  func (v *Value) MSI(optionalDefault ...map[string]interface{}) map[string]interface{} {
    10  	if s, ok := v.data.(map[string]interface{}); ok {
    11  		return s
    12  	}
    13  	if s, ok := v.data.(Map); ok {
    14  		return map[string]interface{}(s)
    15  	}
    16  	if len(optionalDefault) == 1 {
    17  		return optionalDefault[0]
    18  	}
    19  	return nil
    20  }
    21  
    22  // MustMSI gets the value as a map[string]interface{}.
    23  //
    24  // Panics if the object is not a map[string]interface{}.
    25  func (v *Value) MustMSI() map[string]interface{} {
    26  	if s, ok := v.data.(Map); ok {
    27  		return map[string]interface{}(s)
    28  	}
    29  	return v.data.(map[string]interface{})
    30  }
    31  
    32  // MSISlice gets the value as a []map[string]interface{}, returns the optionalDefault
    33  // value or nil if the value is not a []map[string]interface{}.
    34  func (v *Value) MSISlice(optionalDefault ...[]map[string]interface{}) []map[string]interface{} {
    35  	if s, ok := v.data.([]map[string]interface{}); ok {
    36  		return s
    37  	}
    38  
    39  	s := v.ObjxMapSlice()
    40  	if s == nil {
    41  		if len(optionalDefault) == 1 {
    42  			return optionalDefault[0]
    43  		}
    44  		return nil
    45  	}
    46  
    47  	result := make([]map[string]interface{}, len(s))
    48  	for i := range s {
    49  		result[i] = s[i].Value().MSI()
    50  	}
    51  	return result
    52  }
    53  
    54  // MustMSISlice gets the value as a []map[string]interface{}.
    55  //
    56  // Panics if the object is not a []map[string]interface{}.
    57  func (v *Value) MustMSISlice() []map[string]interface{} {
    58  	if s := v.MSISlice(); s != nil {
    59  		return s
    60  	}
    61  
    62  	return v.data.([]map[string]interface{})
    63  }
    64  
    65  // IsMSI gets whether the object contained is a map[string]interface{} or not.
    66  func (v *Value) IsMSI() bool {
    67  	_, ok := v.data.(map[string]interface{})
    68  	if !ok {
    69  		_, ok = v.data.(Map)
    70  	}
    71  	return ok
    72  }
    73  
    74  // IsMSISlice gets whether the object contained is a []map[string]interface{} or not.
    75  func (v *Value) IsMSISlice() bool {
    76  	_, ok := v.data.([]map[string]interface{})
    77  	if !ok {
    78  		_, ok = v.data.([]Map)
    79  	}
    80  	return ok
    81  }
    82  
    83  // EachMSI calls the specified callback for each object
    84  // in the []map[string]interface{}.
    85  //
    86  // Panics if the object is the wrong type.
    87  func (v *Value) EachMSI(callback func(int, map[string]interface{}) bool) *Value {
    88  	for index, val := range v.MustMSISlice() {
    89  		carryon := callback(index, val)
    90  		if !carryon {
    91  			break
    92  		}
    93  	}
    94  	return v
    95  }
    96  
    97  // WhereMSI uses the specified decider function to select items
    98  // from the []map[string]interface{}.  The object contained in the result will contain
    99  // only the selected items.
   100  func (v *Value) WhereMSI(decider func(int, map[string]interface{}) bool) *Value {
   101  	var selected []map[string]interface{}
   102  	v.EachMSI(func(index int, val map[string]interface{}) bool {
   103  		shouldSelect := decider(index, val)
   104  		if !shouldSelect {
   105  			selected = append(selected, val)
   106  		}
   107  		return true
   108  	})
   109  	return &Value{data: selected}
   110  }
   111  
   112  // GroupMSI uses the specified grouper function to group the items
   113  // keyed by the return of the grouper.  The object contained in the
   114  // result will contain a map[string][]map[string]interface{}.
   115  func (v *Value) GroupMSI(grouper func(int, map[string]interface{}) string) *Value {
   116  	groups := make(map[string][]map[string]interface{})
   117  	v.EachMSI(func(index int, val map[string]interface{}) bool {
   118  		group := grouper(index, val)
   119  		if _, ok := groups[group]; !ok {
   120  			groups[group] = make([]map[string]interface{}, 0)
   121  		}
   122  		groups[group] = append(groups[group], val)
   123  		return true
   124  	})
   125  	return &Value{data: groups}
   126  }
   127  
   128  // ReplaceMSI uses the specified function to replace each map[string]interface{}s
   129  // by iterating each item.  The data in the returned result will be a
   130  // []map[string]interface{} containing the replaced items.
   131  func (v *Value) ReplaceMSI(replacer func(int, map[string]interface{}) map[string]interface{}) *Value {
   132  	arr := v.MustMSISlice()
   133  	replaced := make([]map[string]interface{}, len(arr))
   134  	v.EachMSI(func(index int, val map[string]interface{}) bool {
   135  		replaced[index] = replacer(index, val)
   136  		return true
   137  	})
   138  	return &Value{data: replaced}
   139  }
   140  
   141  // CollectMSI uses the specified collector function to collect a value
   142  // for each of the map[string]interface{}s in the slice.  The data returned will be a
   143  // []interface{}.
   144  func (v *Value) CollectMSI(collector func(int, map[string]interface{}) interface{}) *Value {
   145  	arr := v.MustMSISlice()
   146  	collected := make([]interface{}, len(arr))
   147  	v.EachMSI(func(index int, val map[string]interface{}) bool {
   148  		collected[index] = collector(index, val)
   149  		return true
   150  	})
   151  	return &Value{data: collected}
   152  }
   153  
   154  /*
   155     ObjxMap ((Map) and [](Map))
   156  */
   157  
   158  // ObjxMap gets the value as a (Map), returns the optionalDefault
   159  // value or a system default object if the value is the wrong type.
   160  func (v *Value) ObjxMap(optionalDefault ...(Map)) Map {
   161  	if s, ok := v.data.((Map)); ok {
   162  		return s
   163  	}
   164  	if s, ok := v.data.(map[string]interface{}); ok {
   165  		return s
   166  	}
   167  	if len(optionalDefault) == 1 {
   168  		return optionalDefault[0]
   169  	}
   170  	return New(nil)
   171  }
   172  
   173  // MustObjxMap gets the value as a (Map).
   174  //
   175  // Panics if the object is not a (Map).
   176  func (v *Value) MustObjxMap() Map {
   177  	if s, ok := v.data.(map[string]interface{}); ok {
   178  		return s
   179  	}
   180  	return v.data.((Map))
   181  }
   182  
   183  // ObjxMapSlice gets the value as a [](Map), returns the optionalDefault
   184  // value or nil if the value is not a [](Map).
   185  func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
   186  	if s, ok := v.data.([]Map); ok {
   187  		return s
   188  	}
   189  
   190  	if s, ok := v.data.([]map[string]interface{}); ok {
   191  		result := make([]Map, len(s))
   192  		for i := range s {
   193  			result[i] = s[i]
   194  		}
   195  		return result
   196  	}
   197  
   198  	s, ok := v.data.([]interface{})
   199  	if !ok {
   200  		if len(optionalDefault) == 1 {
   201  			return optionalDefault[0]
   202  		}
   203  		return nil
   204  	}
   205  
   206  	result := make([]Map, len(s))
   207  	for i := range s {
   208  		switch s[i].(type) {
   209  		case Map:
   210  			result[i] = s[i].(Map)
   211  		case map[string]interface{}:
   212  			result[i] = New(s[i])
   213  		default:
   214  			return nil
   215  		}
   216  	}
   217  	return result
   218  }
   219  
   220  // MustObjxMapSlice gets the value as a [](Map).
   221  //
   222  // Panics if the object is not a [](Map).
   223  func (v *Value) MustObjxMapSlice() [](Map) {
   224  	if s := v.ObjxMapSlice(); s != nil {
   225  		return s
   226  	}
   227  	return v.data.([](Map))
   228  }
   229  
   230  // IsObjxMap gets whether the object contained is a (Map) or not.
   231  func (v *Value) IsObjxMap() bool {
   232  	_, ok := v.data.((Map))
   233  	if !ok {
   234  		_, ok = v.data.(map[string]interface{})
   235  	}
   236  	return ok
   237  }
   238  
   239  // IsObjxMapSlice gets whether the object contained is a [](Map) or not.
   240  func (v *Value) IsObjxMapSlice() bool {
   241  	_, ok := v.data.([](Map))
   242  	if !ok {
   243  		_, ok = v.data.([]map[string]interface{})
   244  	}
   245  	return ok
   246  }
   247  
   248  // EachObjxMap calls the specified callback for each object
   249  // in the [](Map).
   250  //
   251  // Panics if the object is the wrong type.
   252  func (v *Value) EachObjxMap(callback func(int, Map) bool) *Value {
   253  	for index, val := range v.MustObjxMapSlice() {
   254  		carryon := callback(index, val)
   255  		if !carryon {
   256  			break
   257  		}
   258  	}
   259  	return v
   260  }
   261  
   262  // WhereObjxMap uses the specified decider function to select items
   263  // from the [](Map).  The object contained in the result will contain
   264  // only the selected items.
   265  func (v *Value) WhereObjxMap(decider func(int, Map) bool) *Value {
   266  	var selected [](Map)
   267  	v.EachObjxMap(func(index int, val Map) bool {
   268  		shouldSelect := decider(index, val)
   269  		if !shouldSelect {
   270  			selected = append(selected, val)
   271  		}
   272  		return true
   273  	})
   274  	return &Value{data: selected}
   275  }
   276  
   277  // GroupObjxMap uses the specified grouper function to group the items
   278  // keyed by the return of the grouper.  The object contained in the
   279  // result will contain a map[string][](Map).
   280  func (v *Value) GroupObjxMap(grouper func(int, Map) string) *Value {
   281  	groups := make(map[string][](Map))
   282  	v.EachObjxMap(func(index int, val Map) bool {
   283  		group := grouper(index, val)
   284  		if _, ok := groups[group]; !ok {
   285  			groups[group] = make([](Map), 0)
   286  		}
   287  		groups[group] = append(groups[group], val)
   288  		return true
   289  	})
   290  	return &Value{data: groups}
   291  }
   292  
   293  // ReplaceObjxMap uses the specified function to replace each (Map)s
   294  // by iterating each item.  The data in the returned result will be a
   295  // [](Map) containing the replaced items.
   296  func (v *Value) ReplaceObjxMap(replacer func(int, Map) Map) *Value {
   297  	arr := v.MustObjxMapSlice()
   298  	replaced := make([](Map), len(arr))
   299  	v.EachObjxMap(func(index int, val Map) bool {
   300  		replaced[index] = replacer(index, val)
   301  		return true
   302  	})
   303  	return &Value{data: replaced}
   304  }
   305  
   306  // CollectObjxMap uses the specified collector function to collect a value
   307  // for each of the (Map)s in the slice.  The data returned will be a
   308  // []interface{}.
   309  func (v *Value) CollectObjxMap(collector func(int, Map) interface{}) *Value {
   310  	arr := v.MustObjxMapSlice()
   311  	collected := make([]interface{}, len(arr))
   312  	v.EachObjxMap(func(index int, val Map) bool {
   313  		collected[index] = collector(index, val)
   314  		return true
   315  	})
   316  	return &Value{data: collected}
   317  }