MCPcopy
hub / github.com/spf13/viper / mergeMaps

Function mergeMaps

viper.go:1806–1882  ·  view source on GitHub ↗

mergeMaps merges two maps. The `itgt` parameter is for handling go-yaml's insistence on parsing nested structures as `map[any]any` instead of using a `string` as the key for nest structures beyond one level deep. Both map types are supported as there is a go-yaml fork that uses `map[string]any` inst

(src, tgt map[string]any, itgt map[any]any)

Source from the content-addressed store, hash-verified

1804// deep. Both map types are supported as there is a go-yaml fork that uses
1805// `map[string]any` instead.
1806func mergeMaps(src, tgt map[string]any, itgt map[any]any) {
1807 for sk, sv := range src {
1808 tk := keyExists(sk, tgt)
1809 if tk == "" {
1810 v.logger.Debug("", "tk", "\"\"", fmt.Sprintf("tgt[%s]", sk), sv)
1811 tgt[sk] = sv
1812 if itgt != nil {
1813 itgt[sk] = sv
1814 }
1815 continue
1816 }
1817
1818 tv, ok := tgt[tk]
1819 if !ok {
1820 v.logger.Debug("", fmt.Sprintf("ok[%s]", tk), false, fmt.Sprintf("tgt[%s]", sk), sv)
1821 tgt[sk] = sv
1822 if itgt != nil {
1823 itgt[sk] = sv
1824 }
1825 continue
1826 }
1827
1828 svType := reflect.TypeOf(sv)
1829 tvType := reflect.TypeOf(tv)
1830
1831 v.logger.Debug(
1832 "processing",
1833 "key", sk,
1834 "st", svType,
1835 "tt", tvType,
1836 "sv", sv,
1837 "tv", tv,
1838 )
1839
1840 switch ttv := tv.(type) {
1841 case map[any]any:
1842 v.logger.Debug("merging maps (must convert)")
1843 tsv, ok := sv.(map[any]any)
1844 if !ok {
1845 v.logger.Error(
1846 "Could not cast sv to map[any]any",
1847 "key", sk,
1848 "st", svType,
1849 "tt", tvType,
1850 "sv", sv,
1851 "tv", tv,
1852 )
1853 continue
1854 }
1855
1856 ssv := castToMapStringInterface(tsv)
1857 stv := castToMapStringInterface(ttv)
1858 mergeMaps(ssv, stv, ttv)
1859 case map[string]any:
1860 v.logger.Debug("merging maps")
1861 tsv, ok := sv.(map[string]any)
1862 if !ok {
1863 v.logger.Error(

Callers 1

MergeConfigMapMethod · 0.85

Calls 4

keyExistsFunction · 0.85
castToMapStringInterfaceFunction · 0.85
DebugMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected