forked from sourcegraph/srclib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo_source_unit.go
56 lines (49 loc) · 1.52 KB
/
repo_source_unit.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
package unit
import (
"encoding/json"
"sourcegraph.com/sourcegraph/srclib/util/sqltypes"
)
// A RepoSourceUnit is the "concrete" form of SourceUnit that includes
// information about which repository (and commit) the source unit exists in. In
// general, type SourceUnit is used during analysis of a single source unit and
// type RepoSourceUnit is used afterwards (either in cross-source-unit analysis,
// such as cross-reference resolution, or in after-the-fact DB/API queries).
type RepoSourceUnit struct {
Repo string
CommitID string `db:"commit_id"`
UnitType string `db:"unit_type"`
Unit string
// Private is true if this is from a private repository.
Private bool
// Data is the JSON of the underlying SourceUnit.
Data sqltypes.JSON
}
// NewRepoSourceUnit creates an equivalent RepoSourceUnit from a
// SourceUnit.
//
// It does not set the returned source unit's Private field (because
// it can't tell if it is private from the underlying source unit
// alone).
//
// It also doesn't set CommitID (for the same reason).
func NewRepoSourceUnit(u *SourceUnit) (*RepoSourceUnit, error) {
unitJSON, err := json.Marshal(u)
if err != nil {
return nil, err
}
return &RepoSourceUnit{
Repo: u.Repo,
UnitType: u.Type,
Unit: u.Name,
Data: unitJSON,
}, nil
}
// SourceUnit decodes u's Data JSON field to the SourceUnit it
// represents.
func (u *RepoSourceUnit) SourceUnit() (*SourceUnit, error) {
var u2 *SourceUnit
if err := json.Unmarshal(u.Data, &u2); err != nil {
return nil, err
}
return u2, nil
}