-
Notifications
You must be signed in to change notification settings - Fork 1
/
ref.go
66 lines (54 loc) · 1.45 KB
/
ref.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package goref
import (
"encoding/json"
"fmt"
pb "github.com/korfuri/goref/proto"
)
// A Ref is a reference to an identifier whose definition lives in
// another package.
type Ref struct {
// Type of reference
RefType
// Where this reference points from, i.e. where the identifier
// was used in another package.
FromPosition Position
// Where this reference points to, i.e. where the definition
// is
ToPosition Position
// What identifier points to this Ref
FromIdent string
// What identifier this Ref points to
ToIdent string
// What package contains what the identifier refers to
ToPackage *Package
// What package the ref is from, i.e. what foreign package was
// this identifier used in.
FromPackage *Package
}
func (r *Ref) String() string {
return fmt.Sprintf("%s of to:`%s.%s` at %s by from:`%s.%s` at %s",
r.RefType,
r.ToPackage, r.ToIdent, r.ToPosition,
r.FromPackage, r.FromIdent, r.FromPosition)
}
// MarshalJSON implements encoding/json.Marshaler interface
func (r Ref) MarshalJSON() ([]byte, error) {
return json.Marshal(r.ToProto())
}
// ToProto marshals a Ref as a pb.Ref
func (r Ref) ToProto() *pb.Ref {
return &pb.Ref{
Version: r.FromPackage.Version,
From: &pb.Location{
Position: r.FromPosition.ToProto(),
Package: r.FromPackage.Path,
Ident: r.FromIdent,
},
To: &pb.Location{
Position: r.ToPosition.ToProto(),
Package: r.ToPackage.Path,
Ident: r.ToIdent,
},
Type: pb.Type(r.RefType),
}
}