| 1 |
// This file is part of go-properties-orderedmap library. |
| 2 |
// |
| 3 |
// SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies |
| 4 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
|
| 6 |
package properties |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
// DebugExpandPropsInString outputs the substitutions made by |
| 14 |
// ExpandPropsInString for debugging purposes. |
| 15 |
func (m *Map) DebugExpandPropsInString(str string) string { |
| 16 |
return m.expandProps(str, true) |
| 17 |
} |
| 18 |
|
| 19 |
func (m *Map) expandProps(str string, debug bool) string { |
| 20 |
debug = debug || m.Debug |
| 21 |
for i := range 10 { |
| 22 |
if debug { |
| 23 |
fmt.Printf("pass %d: %s\n", i, str) |
| 24 |
} |
| 25 |
newStr := str |
| 26 |
for key, value := range m.kv { |
| 27 |
if debug && strings.Contains(newStr, "{"+key+"}") { |
| 28 |
fmt.Printf(" Replacing %s -> %s\n", key, value) |
| 29 |
} |
| 30 |
newStr = strings.Replace(newStr, "{"+key+"}", value, -1) |
| 31 |
} |
| 32 |
if str == newStr { |
| 33 |
break |
| 34 |
} |
| 35 |
str = newStr |
| 36 |
} |
| 37 |
return str |
| 38 |
} |
| 39 |
|
| 40 |
// Dump returns a representation of the map in golang source format |
| 41 |
func (m *Map) Dump() string { |
| 42 |
var res strings.Builder |
| 43 |
res.WriteString("properties.Map{\n") |
| 44 |
for _, k := range m.o { |
| 45 |
res.WriteString(fmt.Sprintf(" \"%s\": \"%s\",\n", strings.Replace(k, `"`, `\"`, -1), strings.Replace(m.Get(k), `"`, `\"`, -1))) |
| 46 |
} |
| 47 |
res.WriteString("}") |
| 48 |
return res.String() |
| 49 |
} |