File tree 2 files changed +41
-4
lines changed
2 files changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ package properties
71
71
import (
72
72
"fmt"
73
73
"io/ioutil"
74
+ "math/rand"
74
75
"os"
75
76
"reflect"
76
77
"regexp"
@@ -395,6 +396,39 @@ func (m *Map) ExpandPropsInString(str string) string {
395
396
return m .expandProps (str , false )
396
397
}
397
398
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
+
398
432
// Merge merges other Maps into this one. Each key/value of the merged Maps replaces
399
433
// the key/value present in the original Map.
400
434
func (m * Map ) Merge (sources ... * Map ) * Map {
Original file line number Diff line number Diff line change @@ -74,15 +74,18 @@ func TestPropertiesTestTxt(t *testing.T) {
74
74
}
75
75
}
76
76
77
- func TestExpandPropsInString (t * testing.T ) {
77
+ func TestExpandPropsInStringAndMissingCheck (t * testing.T ) {
78
78
aMap := NewMap ()
79
79
aMap .Set ("key1" , "42" )
80
80
aMap .Set ("key2" , "{key1}" )
81
+ aMap .Set ("key3" , "{key4}" )
81
82
82
- str := " {key1} == {key2} == true"
83
+ require . Equal ( t , "42 == 42 == true" , aMap . ExpandPropsInString ( " {key1} == {key2} == true"))
83
84
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}" ))
86
89
}
87
90
88
91
func TestExpandPropsInString2 (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments