func makeGraphData(t testing.TB, n int) graph.Output { data := graph.Output{} if n > 0 { data.Defs = make([]*graph.Def, n) data.Refs = make([]*graph.Ref, n) } for i := 0; i < n; i++ { data.Defs[i] = &graph.Def{ DefKey: graph.DefKey{Path: fmt.Sprintf("def-path-%d", i)}, Name: fmt.Sprintf("def-name-%d", i), Kind: "mykind", DefStart: uint32((i % 53) * 37), DefEnd: uint32((i%53)*37 + (i % 20)), File: fmt.Sprintf("dir%d/subdir%d/subsubdir%d/file-%d.foo", i%5, i%3, i%7, i%5), Exported: i%5 == 0, Local: i%3 == 0, Data: []byte(`"` + strings.Repeat("abcd", 50) + `"`), } data.Refs[i] = &graph.Ref{ DefPath: fmt.Sprintf("ref-path-%d", i), Def: i%5 == 0, Start: uint32((i % 51) * 39), End: uint32((i%51)*37 + (int(i) % 18)), File: fmt.Sprintf("dir%d/subdir%d/subsubdir%d/file-%d.foo", i%3, i%5, i%7, i%5), } if i%3 == 0 { data.Refs[i].DefUnit = fmt.Sprintf("def-unit-%d", i%17) data.Refs[i].DefUnitType = fmt.Sprintf("def-unit-type-%d", i%3) if i%7 == 0 { data.Refs[i].DefRepo = fmt.Sprintf("def-repo-%d", i%13) } } } return data }
func (c *GraphContext) transform(raw *RawOutput, unit *unit.SourceUnit) *graph.Output { var out graph.Output for _, def := range raw.Defs { out.Defs = append(out.Defs, c.transformDef(def)) if doc := c.transformDefDoc(def); doc != nil { out.Docs = append(out.Docs, doc) } } for _, ref := range raw.Refs { if outRef, err := c.transformRef(ref); err == nil { out.Refs = append(out.Refs, outRef) } else { log.Printf("Could not transform ref %v: %s", ref, err) } } return &out }
func Graph(unit *unit.SourceUnit) (*graph.Output, error) { pkg, err := UnitDataAsBuildPackage(unit) if err != nil { return nil, err } o, err := doGraph(pkg) if err != nil { return nil, err } o2 := graph.Output{} for _, gs := range o.Defs { d, err := convertGoDef(gs) if err != nil { return nil, err } if d != nil { o2.Defs = append(o2.Defs, d) } } for _, gr := range o.Refs { r, err := convertGoRef(gr) if err != nil { return nil, err } if r != nil { o2.Refs = append(o2.Refs, r) } } for _, gd := range o.Docs { d, err := convertGoDoc(gd) if err != nil { return nil, err } if d != nil { o2.Docs = append(o2.Docs, d) } } return &o2, nil }