0x4a52466c696e74 il y a 2 ans
Parent
commit
8c43b304a7
1 fichiers modifiés avec 33 ajouts et 4 suppressions
  1. 33 4
      json/map.go

+ 33 - 4
json/map.go

@@ -160,11 +160,26 @@ func (s Map) String(key, defaultVal string) string {
 	return defaultVal
 }
 
-func (s Map) StringMust(key, defaultVal string) string {
-	if iface, check := s[key]; check {
-		return fmt.Sprint(iface)
+func (s Map) EachSlice(key string, each func(int, any) bool) {
+	sl := s.Slice(key, []any{})
+	for i, val := range sl {
+		if !each(i, val) {
+			return
+		}
 	}
-	return defaultVal
+}
+
+func (s Map) EachMap(key string, each func(int, Map) bool) {
+	s.EachSlice(key, func(i int, val any) bool {
+		switch val.(type) {
+		case map[string]any:
+			return each(i, Map(val.(map[string]any)))
+		case Map:
+			return each(i, val.(Map))
+		default:
+			return true
+		}
+	})
 }
 
 func (s Map) StringWithError(key, defaultVal string) (string, error) {
@@ -251,6 +266,20 @@ func (s Map) JSONIndent(prefix, indent string) (res []byte) {
 	return
 }
 
+func (s Map) JSONString() (res string) {
+	if data, err := json.Marshal(s); err == nil {
+		res = string(data)
+	}
+	return
+}
+
+func (s Map) JSONPrettyString() (res string) {
+	if data, err := json.MarshalIndent(s, "", "\t"); err == nil {
+		res = string(data)
+	}
+	return
+}
+
 // ToMap returns map[string]interface{} of the self object
 func (s Map) ToMap() map[string]interface{} { return map[string]interface{}(s) }