Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit c991032

Browse files
Denys Smirnovdennwc
authored andcommitted
nodes: provide extra type safety for unique keys
Signed-off-by: Denys Smirnov <[email protected]>
1 parent 5f84beb commit c991032

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

uast/nodes/node.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Node interface {
5858
// * Bool
5959
type Value interface {
6060
Node
61+
Comparable
6162
isValue() // to limit possible types
6263
}
6364

@@ -418,8 +419,9 @@ func (m *Array) SetNode(n Node) error {
418419
// String is a string value used in tree fields.
419420
type String string
420421

421-
func (String) isNode() {}
422-
func (String) isValue() {}
422+
func (String) isNode() {}
423+
func (String) isValue() {}
424+
func (String) isComparable() {}
423425
func (String) Kind() Kind {
424426
return KindString
425427
}
@@ -466,8 +468,9 @@ func (v String) SameAs(n External) bool {
466468
// Int is a integer value used in tree fields.
467469
type Int int64
468470

469-
func (Int) isNode() {}
470-
func (Int) isValue() {}
471+
func (Int) isNode() {}
472+
func (Int) isValue() {}
473+
func (Int) isComparable() {}
471474
func (Int) Kind() Kind {
472475
return KindInt
473476
}
@@ -519,8 +522,9 @@ func (v Int) SameAs(n External) bool {
519522
// Uint is a unsigned integer value used in tree fields.
520523
type Uint uint64
521524

522-
func (Uint) isNode() {}
523-
func (Uint) isValue() {}
525+
func (Uint) isNode() {}
526+
func (Uint) isValue() {}
527+
func (Uint) isComparable() {}
524528
func (Uint) Kind() Kind {
525529
return KindUint
526530
}
@@ -572,8 +576,9 @@ func (v Uint) SameAs(n External) bool {
572576
// Float is a floating point value used in tree fields.
573577
type Float float64
574578

575-
func (Float) isNode() {}
576-
func (Float) isValue() {}
579+
func (Float) isNode() {}
580+
func (Float) isValue() {}
581+
func (Float) isComparable() {}
577582
func (Float) Kind() Kind {
578583
return KindFloat
579584
}
@@ -620,8 +625,9 @@ func (v Float) SameAs(n External) bool {
620625
// Bool is a boolean value used in tree fields.
621626
type Bool bool
622627

623-
func (Bool) isNode() {}
624-
func (Bool) isValue() {}
628+
func (Bool) isNode() {}
629+
func (Bool) isValue() {}
630+
func (Bool) isComparable() {}
625631
func (Bool) Kind() Kind {
626632
return KindBool
627633
}
@@ -907,10 +913,24 @@ func pointerOf(n Node) uintptr {
907913
}
908914

909915
type arrayPtr uintptr
916+
917+
func (arrayPtr) isComparable() {}
918+
910919
type mapPtr uintptr
911920

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+
912932
// 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 {
914934
switch n := n.(type) {
915935
case nil:
916936
return nil
@@ -925,6 +945,6 @@ func UniqueKey(n Node) interface{} {
925945
case Array:
926946
return arrayPtr(ptr)
927947
}
928-
return ptr
948+
return unkPtr(ptr)
929949
}
930950
}

0 commit comments

Comments
 (0)