-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package lambroll | ||
|
||
func isEmptyValue(value interface{}) bool { | ||
switch v := value.(type) { | ||
case nil: | ||
return true | ||
case string: | ||
return v == "" | ||
case bool: | ||
return !v | ||
case map[string]interface{}: | ||
return len(v) == 0 | ||
case []interface{}: | ||
return len(v) == 0 | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func omitEmptyValues(data interface{}) interface{} { | ||
switch v := data.(type) { | ||
case map[string]interface{}: | ||
nonEmptyMap := make(map[string]interface{}) | ||
for key, value := range v { | ||
nonEmptyValue := omitEmptyValues(value) | ||
if !isEmptyValue(nonEmptyValue) { | ||
nonEmptyMap[key] = nonEmptyValue | ||
} | ||
} | ||
if len(nonEmptyMap) != 0 { | ||
return nonEmptyMap | ||
} | ||
case []interface{}: | ||
nonEmptyList := make([]interface{}, 0) | ||
for _, value := range v { | ||
nonEmptyValue := omitEmptyValues(value) | ||
if !isEmptyValue(nonEmptyValue) { | ||
nonEmptyList = append(nonEmptyList, nonEmptyValue) | ||
} | ||
} | ||
if len(nonEmptyList) != 0 { | ||
return nonEmptyList | ||
} | ||
default: | ||
if !isEmptyValue(v) { | ||
return v | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters