@@ -58,6 +58,7 @@ type Node interface {
58
58
// * Bool
59
59
type Value interface {
60
60
Node
61
+ Comparable
61
62
isValue () // to limit possible types
62
63
}
63
64
@@ -418,8 +419,9 @@ func (m *Array) SetNode(n Node) error {
418
419
// String is a string value used in tree fields.
419
420
type String string
420
421
421
- func (String ) isNode () {}
422
- func (String ) isValue () {}
422
+ func (String ) isNode () {}
423
+ func (String ) isValue () {}
424
+ func (String ) isComparable () {}
423
425
func (String ) Kind () Kind {
424
426
return KindString
425
427
}
@@ -466,8 +468,9 @@ func (v String) SameAs(n External) bool {
466
468
// Int is a integer value used in tree fields.
467
469
type Int int64
468
470
469
- func (Int ) isNode () {}
470
- func (Int ) isValue () {}
471
+ func (Int ) isNode () {}
472
+ func (Int ) isValue () {}
473
+ func (Int ) isComparable () {}
471
474
func (Int ) Kind () Kind {
472
475
return KindInt
473
476
}
@@ -519,8 +522,9 @@ func (v Int) SameAs(n External) bool {
519
522
// Uint is a unsigned integer value used in tree fields.
520
523
type Uint uint64
521
524
522
- func (Uint ) isNode () {}
523
- func (Uint ) isValue () {}
525
+ func (Uint ) isNode () {}
526
+ func (Uint ) isValue () {}
527
+ func (Uint ) isComparable () {}
524
528
func (Uint ) Kind () Kind {
525
529
return KindUint
526
530
}
@@ -572,8 +576,9 @@ func (v Uint) SameAs(n External) bool {
572
576
// Float is a floating point value used in tree fields.
573
577
type Float float64
574
578
575
- func (Float ) isNode () {}
576
- func (Float ) isValue () {}
579
+ func (Float ) isNode () {}
580
+ func (Float ) isValue () {}
581
+ func (Float ) isComparable () {}
577
582
func (Float ) Kind () Kind {
578
583
return KindFloat
579
584
}
@@ -620,8 +625,9 @@ func (v Float) SameAs(n External) bool {
620
625
// Bool is a boolean value used in tree fields.
621
626
type Bool bool
622
627
623
- func (Bool ) isNode () {}
624
- func (Bool ) isValue () {}
628
+ func (Bool ) isNode () {}
629
+ func (Bool ) isValue () {}
630
+ func (Bool ) isComparable () {}
625
631
func (Bool ) Kind () Kind {
626
632
return KindBool
627
633
}
@@ -907,10 +913,24 @@ func pointerOf(n Node) uintptr {
907
913
}
908
914
909
915
type arrayPtr uintptr
916
+
917
+ func (arrayPtr ) isComparable () {}
918
+
910
919
type mapPtr uintptr
911
920
921
+ func (mapPtr ) isComparable () {}
922
+
923
+ type unkPtr uintptr
924
+
925
+ func (unkPtr ) isComparable () {}
926
+
927
+ // Comparable is an interface for comparable values that are guaranteed to be safely used as map keys.
928
+ type Comparable interface {
929
+ isComparable ()
930
+ }
931
+
912
932
// UniqueKey returns a unique key of the node in the current tree. The key can be used in maps.
913
- func UniqueKey (n Node ) interface {} {
933
+ func UniqueKey (n Node ) Comparable {
914
934
switch n := n .(type ) {
915
935
case nil :
916
936
return nil
@@ -925,6 +945,6 @@ func UniqueKey(n Node) interface{} {
925
945
case Array :
926
946
return arrayPtr (ptr )
927
947
}
928
- return ptr
948
+ return unkPtr ( ptr )
929
949
}
930
950
}
0 commit comments