Skip to content

Commit 7e1498e

Browse files
committed
Added IsProertyMissingInExpandPropsInString method
1 parent 308241b commit 7e1498e

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

Diff for: properties.go

+34
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ package properties
7171
import (
7272
"fmt"
7373
"io/ioutil"
74+
"math/rand"
7475
"os"
7576
"reflect"
7677
"regexp"
@@ -395,6 +396,39 @@ func (m *Map) ExpandPropsInString(str string) string {
395396
return m.expandProps(str, false)
396397
}
397398

399+
// IsProertyMissingInExpandPropsInString checks if a property 'prop' is missing
400+
// when the ExpandPropsInString method is applied to the input string 'str'.
401+
// This method returns false if the 'prop' is defined in the map
402+
// or if 'prop' is not used in the string expansion of 'str', otherwise
403+
// the method returns true.
404+
func (m *Map) IsProertyMissingInExpandPropsInString(prop, str string) bool {
405+
if m.ContainsKey(prop) {
406+
return false
407+
}
408+
409+
xm := m.Clone()
410+
411+
// Find a random tag that is not contained in the dictionary and the src pattern
412+
var token string
413+
for {
414+
token = fmt.Sprintf("%d", rand.Int63())
415+
if strings.Contains(str, token) {
416+
continue
417+
}
418+
if xm.ContainsKey(token) {
419+
continue
420+
}
421+
if xm.ContainsValue(token) {
422+
continue
423+
}
424+
break
425+
}
426+
xm.Set(prop, token)
427+
428+
res := xm.expandProps(str, false)
429+
return strings.Contains(res, token)
430+
}
431+
398432
// Merge merges other Maps into this one. Each key/value of the merged Maps replaces
399433
// the key/value present in the original Map.
400434
func (m *Map) Merge(sources ...*Map) *Map {

Diff for: properties_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ func TestPropertiesTestTxt(t *testing.T) {
7474
}
7575
}
7676

77-
func TestExpandPropsInString(t *testing.T) {
77+
func TestExpandPropsInStringAndMissingCheck(t *testing.T) {
7878
aMap := NewMap()
7979
aMap.Set("key1", "42")
8080
aMap.Set("key2", "{key1}")
81+
aMap.Set("key3", "{key4}")
8182

82-
str := "{key1} == {key2} == true"
83+
require.Equal(t, "42 == 42 == true", aMap.ExpandPropsInString("{key1} == {key2} == true"))
8384

84-
str = aMap.ExpandPropsInString(str)
85-
require.Equal(t, "42 == 42 == true", str)
85+
require.False(t, aMap.IsProertyMissingInExpandPropsInString("key3", "{key1} == {key2} == true"))
86+
require.False(t, aMap.IsProertyMissingInExpandPropsInString("key1", "{key1} == {key2} == true"))
87+
require.True(t, aMap.IsProertyMissingInExpandPropsInString("key4", "{key4} == {key2}"))
88+
require.True(t, aMap.IsProertyMissingInExpandPropsInString("key4", "{key3} == {key2}"))
8689
}
8790

8891
func TestExpandPropsInString2(t *testing.T) {

0 commit comments

Comments
 (0)