variables.go 664 B

123456789101112131415161718192021
  1. // Package variables provides utility functions to work with key-value maps such
  2. // as the ones present in the metadata headers of documents and templates.
  3. package variables
  4. // Coalesce merges key-value pairs from all the maps passed as arguments onto a
  5. // single map. The values of keys in earlier maps have priority over later maps.
  6. //
  7. // This function does not operate recursively on nested maps. In other words,
  8. // only the keys at the root level are checked and/or taken.
  9. func Coalesce(maps ...map[string]any) map[string]any {
  10. ret := map[string]any{}
  11. for i := len(maps) - 1; i >= 0; i-- {
  12. for k, v := range maps[i] {
  13. ret[k] = v
  14. }
  15. }
  16. return ret
  17. }