uast.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. package uast
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io"
  8. goioutil "io/ioutil"
  9. "os"
  10. "path"
  11. "runtime"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/gogo/protobuf/proto"
  16. "github.com/jeffail/tunny"
  17. "gopkg.in/bblfsh/client-go.v2"
  18. "gopkg.in/bblfsh/sdk.v1/protocol"
  19. "gopkg.in/bblfsh/sdk.v1/uast"
  20. "gopkg.in/src-d/enry.v1"
  21. "gopkg.in/src-d/go-git.v4"
  22. "gopkg.in/src-d/go-git.v4/plumbing"
  23. "gopkg.in/src-d/go-git.v4/plumbing/object"
  24. "gopkg.in/src-d/go-git.v4/utils/ioutil"
  25. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  26. "gopkg.in/src-d/hercules.v4/internal/core"
  27. "gopkg.in/src-d/hercules.v4/internal/pb"
  28. items "gopkg.in/src-d/hercules.v4/internal/plumbing"
  29. )
  30. // Extractor retrieves UASTs from Babelfish server which correspond to changed files in a commit.
  31. // It is a PipelineItem.
  32. type Extractor struct {
  33. Endpoint string
  34. Context func() (context.Context, context.CancelFunc)
  35. PoolSize int
  36. Languages map[string]bool
  37. FailOnErrors bool
  38. ProcessedFiles map[string]int
  39. clients []*bblfsh.Client
  40. pool *tunny.Pool
  41. }
  42. const (
  43. uastExtractionSkipped = -(1 << 31)
  44. // ConfigUASTEndpoint is the name of the configuration option (Extractor.Configure())
  45. // which sets the Babelfish server address.
  46. ConfigUASTEndpoint = "ConfigUASTEndpoint"
  47. // ConfigUASTTimeout is the name of the configuration option (Extractor.Configure())
  48. // which sets the maximum amount of time to wait for a Babelfish server response.
  49. ConfigUASTTimeout = "ConfigUASTTimeout"
  50. // ConfigUASTPoolSize is the name of the configuration option (Extractor.Configure())
  51. // which sets the number of goroutines to run for UAST parse queries.
  52. ConfigUASTPoolSize = "ConfigUASTPoolSize"
  53. // ConfigUASTFailOnErrors is the name of the configuration option (Extractor.Configure())
  54. // which enables early exit in case of any Babelfish UAST parsing errors.
  55. ConfigUASTFailOnErrors = "ConfigUASTFailOnErrors"
  56. // ConfigUASTLanguages is the name of the configuration option (Extractor.Configure())
  57. // which sets the list of languages to parse. Language names are at
  58. // https://doc.bblf.sh/languages.html Names are joined with a comma ",".
  59. ConfigUASTLanguages = "ConfigUASTLanguages"
  60. // FeatureUast is the name of the Pipeline feature which activates all the items related to UAST.
  61. FeatureUast = "uast"
  62. // DependencyUasts is the name of the dependency provided by Extractor.
  63. DependencyUasts = "uasts"
  64. )
  65. type uastTask struct {
  66. Lock *sync.RWMutex
  67. Dest map[plumbing.Hash]*uast.Node
  68. File *object.File
  69. Errors *[]error
  70. }
  71. type worker struct {
  72. Client *bblfsh.Client
  73. Extractor *Extractor
  74. }
  75. // Process will synchronously perform a job and return the result.
  76. func (w worker) Process(data interface{}) interface{} {
  77. return w.Extractor.extractTask(w.Client, data)
  78. }
  79. func (w worker) BlockUntilReady() {}
  80. func (w worker) Interrupt() {}
  81. func (w worker) Terminate() {}
  82. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  83. func (exr *Extractor) Name() string {
  84. return "UAST"
  85. }
  86. // Provides returns the list of names of entities which are produced by this PipelineItem.
  87. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  88. // to this list. Also used by core.Registry to build the global map of providers.
  89. func (exr *Extractor) Provides() []string {
  90. arr := [...]string{DependencyUasts}
  91. return arr[:]
  92. }
  93. // Requires returns the list of names of entities which are needed by this PipelineItem.
  94. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  95. // entities are Provides() upstream.
  96. func (exr *Extractor) Requires() []string {
  97. arr := [...]string{items.DependencyTreeChanges, items.DependencyBlobCache}
  98. return arr[:]
  99. }
  100. // Features which must be enabled for this PipelineItem to be automatically inserted into the DAG.
  101. func (exr *Extractor) Features() []string {
  102. arr := [...]string{FeatureUast}
  103. return arr[:]
  104. }
  105. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  106. func (exr *Extractor) ListConfigurationOptions() []core.ConfigurationOption {
  107. options := [...]core.ConfigurationOption{{
  108. Name: ConfigUASTEndpoint,
  109. Description: "How many days there are in a single band.",
  110. Flag: "bblfsh",
  111. Type: core.StringConfigurationOption,
  112. Default: "0.0.0.0:9432"}, {
  113. Name: ConfigUASTTimeout,
  114. Description: "Babelfish's server timeout in seconds.",
  115. Flag: "bblfsh-timeout",
  116. Type: core.IntConfigurationOption,
  117. Default: 20}, {
  118. Name: ConfigUASTPoolSize,
  119. Description: "Number of goroutines to extract UASTs.",
  120. Flag: "bblfsh-pool-size",
  121. Type: core.IntConfigurationOption,
  122. Default: runtime.NumCPU() * 2}, {
  123. Name: ConfigUASTFailOnErrors,
  124. Description: "Panic if there is a UAST extraction error.",
  125. Flag: "bblfsh-fail-on-error",
  126. Type: core.BoolConfigurationOption,
  127. Default: false}, {
  128. Name: ConfigUASTLanguages,
  129. Description: "Programming languages from which to extract UASTs. Separated by comma \",\".",
  130. Flag: "languages",
  131. Type: core.StringConfigurationOption,
  132. Default: "Python,Java,Go,JavaScript,Ruby,PHP"},
  133. }
  134. return options[:]
  135. }
  136. // Configure sets the properties previously published by ListConfigurationOptions().
  137. func (exr *Extractor) Configure(facts map[string]interface{}) {
  138. if val, exists := facts[ConfigUASTEndpoint].(string); exists {
  139. exr.Endpoint = val
  140. }
  141. if val, exists := facts[ConfigUASTTimeout].(int); exists {
  142. exr.Context = func() (context.Context, context.CancelFunc) {
  143. return context.WithTimeout(context.Background(),
  144. time.Duration(val)*time.Second)
  145. }
  146. }
  147. if val, exists := facts[ConfigUASTPoolSize].(int); exists {
  148. exr.PoolSize = val
  149. }
  150. if val, exists := facts[ConfigUASTLanguages].(string); exists {
  151. exr.Languages = map[string]bool{}
  152. for _, lang := range strings.Split(val, ",") {
  153. exr.Languages[strings.TrimSpace(lang)] = true
  154. }
  155. }
  156. if val, exists := facts[ConfigUASTFailOnErrors].(bool); exists {
  157. exr.FailOnErrors = val
  158. }
  159. }
  160. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  161. // calls. The repository which is going to be analysed is supplied as an argument.
  162. func (exr *Extractor) Initialize(repository *git.Repository) {
  163. if exr.Context == nil {
  164. exr.Context = func() (context.Context, context.CancelFunc) {
  165. return context.Background(), nil
  166. }
  167. }
  168. poolSize := exr.PoolSize
  169. if poolSize == 0 {
  170. poolSize = runtime.NumCPU()
  171. }
  172. exr.clients = make([]*bblfsh.Client, poolSize)
  173. for i := 0; i < poolSize; i++ {
  174. client, err := bblfsh.NewClient(exr.Endpoint)
  175. if err != nil {
  176. panic(err)
  177. }
  178. exr.clients[i] = client
  179. }
  180. if exr.pool != nil {
  181. exr.pool.Close()
  182. }
  183. {
  184. i := 0
  185. exr.pool = tunny.New(poolSize, func() tunny.Worker {
  186. w := worker{Client: exr.clients[i], Extractor: exr}
  187. i++
  188. return w
  189. })
  190. }
  191. if exr.pool == nil {
  192. panic("UAST goroutine pool was not created")
  193. }
  194. exr.ProcessedFiles = map[string]int{}
  195. if exr.Languages == nil {
  196. exr.Languages = map[string]bool{}
  197. }
  198. }
  199. // Consume runs this PipelineItem on the next commit data.
  200. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  201. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  202. // This function returns the mapping with analysis results. The keys must be the same as
  203. // in Provides(). If there was an error, nil is returned.
  204. func (exr *Extractor) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  205. cache := deps[items.DependencyBlobCache].(map[plumbing.Hash]*object.Blob)
  206. treeDiffs := deps[items.DependencyTreeChanges].(object.Changes)
  207. uasts := map[plumbing.Hash]*uast.Node{}
  208. lock := sync.RWMutex{}
  209. errs := make([]error, 0)
  210. wg := sync.WaitGroup{}
  211. submit := func(change *object.Change) {
  212. {
  213. reader, err := cache[change.To.TreeEntry.Hash].Reader()
  214. if err != nil {
  215. errs = append(errs, err)
  216. return
  217. }
  218. defer ioutil.CheckClose(reader, &err)
  219. buf := new(bytes.Buffer)
  220. if _, err := buf.ReadFrom(reader); err != nil {
  221. errs = append(errs, err)
  222. return
  223. }
  224. lang := enry.GetLanguage(change.To.Name, buf.Bytes())
  225. if _, exists := exr.Languages[lang]; !exists {
  226. exr.ProcessedFiles[change.To.Name] = uastExtractionSkipped
  227. return
  228. }
  229. exr.ProcessedFiles[change.To.Name]++
  230. }
  231. wg.Add(1)
  232. go func(task interface{}) {
  233. exr.pool.Process(task)
  234. wg.Done()
  235. }(uastTask{
  236. Lock: &lock,
  237. Dest: uasts,
  238. File: &object.File{Name: change.To.Name, Blob: *cache[change.To.TreeEntry.Hash]},
  239. Errors: &errs,
  240. })
  241. }
  242. for _, change := range treeDiffs {
  243. action, err := change.Action()
  244. if err != nil {
  245. return nil, err
  246. }
  247. switch action {
  248. case merkletrie.Insert:
  249. submit(change)
  250. case merkletrie.Delete:
  251. continue
  252. case merkletrie.Modify:
  253. submit(change)
  254. }
  255. }
  256. wg.Wait()
  257. if len(errs) > 0 {
  258. msgs := make([]string, len(errs))
  259. for i, err := range errs {
  260. msgs[i] = err.Error()
  261. }
  262. joined := strings.Join(msgs, "\n")
  263. if exr.FailOnErrors {
  264. return nil, errors.New(joined)
  265. }
  266. fmt.Fprintln(os.Stderr, joined)
  267. }
  268. return map[string]interface{}{DependencyUasts: uasts}, nil
  269. }
  270. func (exr *Extractor) extractUAST(
  271. client *bblfsh.Client, file *object.File) (*uast.Node, error) {
  272. request := client.NewParseRequest()
  273. contents, err := file.Contents()
  274. if err != nil {
  275. return nil, err
  276. }
  277. request.Content(contents)
  278. request.Filename(file.Name)
  279. ctx, cancel := exr.Context()
  280. if cancel != nil {
  281. defer cancel()
  282. }
  283. response, err := request.DoWithContext(ctx)
  284. if err != nil {
  285. if strings.Contains("missing driver", err.Error()) {
  286. return nil, nil
  287. }
  288. return nil, err
  289. }
  290. if response.Status != protocol.Ok {
  291. return nil, errors.New(strings.Join(response.Errors, "\n"))
  292. }
  293. if err != nil {
  294. return nil, err
  295. }
  296. return response.UAST, nil
  297. }
  298. func (exr *Extractor) extractTask(client *bblfsh.Client, data interface{}) interface{} {
  299. task := data.(uastTask)
  300. node, err := exr.extractUAST(client, task.File)
  301. task.Lock.Lock()
  302. defer task.Lock.Unlock()
  303. if err != nil {
  304. *task.Errors = append(*task.Errors,
  305. fmt.Errorf("\nfile %s, blob %s: %v", task.File.Name, task.File.Hash.String(), err))
  306. return nil
  307. }
  308. if node != nil {
  309. task.Dest[task.File.Hash] = node
  310. }
  311. return nil
  312. }
  313. // Change is the type of the items in the list of changes which is provided by Changes.
  314. type Change struct {
  315. Before *uast.Node
  316. After *uast.Node
  317. Change *object.Change
  318. }
  319. const (
  320. // DependencyUastChanges is the name of the dependency provided by Changes.
  321. DependencyUastChanges = "changed_uasts"
  322. )
  323. // Changes is a structured analog of TreeDiff: it provides UASTs for every logical change
  324. // in a commit. It is a PipelineItem.
  325. type Changes struct {
  326. cache map[plumbing.Hash]*uast.Node
  327. }
  328. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  329. func (uc *Changes) Name() string {
  330. return "UASTChanges"
  331. }
  332. // Provides returns the list of names of entities which are produced by this PipelineItem.
  333. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  334. // to this list. Also used by core.Registry to build the global map of providers.
  335. func (uc *Changes) Provides() []string {
  336. arr := [...]string{DependencyUastChanges}
  337. return arr[:]
  338. }
  339. // Requires returns the list of names of entities which are needed by this PipelineItem.
  340. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  341. // entities are Provides() upstream.
  342. func (uc *Changes) Requires() []string {
  343. arr := [...]string{DependencyUasts, items.DependencyTreeChanges}
  344. return arr[:]
  345. }
  346. // Features which must be enabled for this PipelineItem to be automatically inserted into the DAG.
  347. func (uc *Changes) Features() []string {
  348. arr := [...]string{FeatureUast}
  349. return arr[:]
  350. }
  351. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  352. func (uc *Changes) ListConfigurationOptions() []core.ConfigurationOption {
  353. return []core.ConfigurationOption{}
  354. }
  355. // Configure sets the properties previously published by ListConfigurationOptions().
  356. func (uc *Changes) Configure(facts map[string]interface{}) {}
  357. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  358. // calls. The repository which is going to be analysed is supplied as an argument.
  359. func (uc *Changes) Initialize(repository *git.Repository) {
  360. uc.cache = map[plumbing.Hash]*uast.Node{}
  361. }
  362. // Consume runs this PipelineItem on the next commit data.
  363. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  364. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  365. // This function returns the mapping with analysis results. The keys must be the same as
  366. // in Provides(). If there was an error, nil is returned.
  367. func (uc *Changes) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  368. uasts := deps[DependencyUasts].(map[plumbing.Hash]*uast.Node)
  369. treeDiffs := deps[items.DependencyTreeChanges].(object.Changes)
  370. commit := make([]Change, 0, len(treeDiffs))
  371. for _, change := range treeDiffs {
  372. action, err := change.Action()
  373. if err != nil {
  374. return nil, err
  375. }
  376. switch action {
  377. case merkletrie.Insert:
  378. hashTo := change.To.TreeEntry.Hash
  379. uastTo := uasts[hashTo]
  380. commit = append(commit, Change{Before: nil, After: uastTo, Change: change})
  381. uc.cache[hashTo] = uastTo
  382. case merkletrie.Delete:
  383. hashFrom := change.From.TreeEntry.Hash
  384. commit = append(commit, Change{Before: uc.cache[hashFrom], After: nil, Change: change})
  385. delete(uc.cache, hashFrom)
  386. case merkletrie.Modify:
  387. hashFrom := change.From.TreeEntry.Hash
  388. hashTo := change.To.TreeEntry.Hash
  389. uastTo := uasts[hashTo]
  390. commit = append(commit, Change{Before: uc.cache[hashFrom], After: uastTo, Change: change})
  391. delete(uc.cache, hashFrom)
  392. uc.cache[hashTo] = uastTo
  393. }
  394. }
  395. return map[string]interface{}{DependencyUastChanges: commit}, nil
  396. }
  397. // ChangesSaver dumps changed files and corresponding UASTs for every commit.
  398. // it is a LeafPipelineItem.
  399. type ChangesSaver struct {
  400. // OutputPath points to the target directory with UASTs
  401. OutputPath string
  402. repository *git.Repository
  403. result [][]Change
  404. }
  405. const (
  406. // ConfigUASTChangesSaverOutputPath is the name of the configuration option
  407. // (ChangesSaver.Configure()) which sets the target directory where to save the files.
  408. ConfigUASTChangesSaverOutputPath = "ChangesSaver.OutputPath"
  409. )
  410. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  411. func (saver *ChangesSaver) Name() string {
  412. return "UASTChangesSaver"
  413. }
  414. // Provides returns the list of names of entities which are produced by this PipelineItem.
  415. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  416. // to this list. Also used by core.Registry to build the global map of providers.
  417. func (saver *ChangesSaver) Provides() []string {
  418. return []string{}
  419. }
  420. // Requires returns the list of names of entities which are needed by this PipelineItem.
  421. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  422. // entities are Provides() upstream.
  423. func (saver *ChangesSaver) Requires() []string {
  424. arr := [...]string{DependencyUastChanges}
  425. return arr[:]
  426. }
  427. // Features which must be enabled for this PipelineItem to be automatically inserted into the DAG.
  428. func (saver *ChangesSaver) Features() []string {
  429. arr := [...]string{FeatureUast}
  430. return arr[:]
  431. }
  432. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  433. func (saver *ChangesSaver) ListConfigurationOptions() []core.ConfigurationOption {
  434. options := [...]core.ConfigurationOption{{
  435. Name: ConfigUASTChangesSaverOutputPath,
  436. Description: "The target directory where to store the changed UAST files.",
  437. Flag: "changed-uast-dir",
  438. Type: core.StringConfigurationOption,
  439. Default: "."},
  440. }
  441. return options[:]
  442. }
  443. // Flag for the command line switch which enables this analysis.
  444. func (saver *ChangesSaver) Flag() string {
  445. return "dump-uast-changes"
  446. }
  447. // Configure sets the properties previously published by ListConfigurationOptions().
  448. func (saver *ChangesSaver) Configure(facts map[string]interface{}) {
  449. if val, exists := facts[ConfigUASTChangesSaverOutputPath]; exists {
  450. saver.OutputPath = val.(string)
  451. }
  452. }
  453. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  454. // calls. The repository which is going to be analysed is supplied as an argument.
  455. func (saver *ChangesSaver) Initialize(repository *git.Repository) {
  456. saver.repository = repository
  457. saver.result = [][]Change{}
  458. }
  459. // Consume runs this PipelineItem on the next commit data.
  460. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  461. // Additionally, "commit" is always present there and represents the analysed *object.Commit.
  462. // This function returns the mapping with analysis results. The keys must be the same as
  463. // in Provides(). If there was an error, nil is returned.
  464. func (saver *ChangesSaver) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  465. changes := deps[DependencyUastChanges].([]Change)
  466. saver.result = append(saver.result, changes)
  467. return nil, nil
  468. }
  469. // Finalize returns the result of the analysis. Further Consume() calls are not expected.
  470. func (saver *ChangesSaver) Finalize() interface{} {
  471. return saver.result
  472. }
  473. // Serialize converts the analysis result as returned by Finalize() to text or bytes.
  474. // The text format is YAML and the bytes format is Protocol Buffers.
  475. func (saver *ChangesSaver) Serialize(result interface{}, binary bool, writer io.Writer) error {
  476. saverResult := result.([][]Change)
  477. fileNames := saver.dumpFiles(saverResult)
  478. if binary {
  479. return saver.serializeBinary(fileNames, writer)
  480. }
  481. saver.serializeText(fileNames, writer)
  482. return nil
  483. }
  484. func (saver *ChangesSaver) dumpFiles(result [][]Change) []*pb.UASTChange {
  485. fileNames := []*pb.UASTChange{}
  486. for i, changes := range result {
  487. for j, change := range changes {
  488. if change.Before == nil || change.After == nil {
  489. continue
  490. }
  491. record := &pb.UASTChange{FileName: change.Change.To.Name}
  492. bs, _ := change.Before.Marshal()
  493. record.UastBefore = path.Join(saver.OutputPath, fmt.Sprintf(
  494. "%d_%d_before_%s.pb", i, j, change.Change.From.TreeEntry.Hash.String()))
  495. goioutil.WriteFile(record.UastBefore, bs, 0666)
  496. blob, _ := saver.repository.BlobObject(change.Change.From.TreeEntry.Hash)
  497. s, _ := (&object.File{Blob: *blob}).Contents()
  498. record.SrcBefore = path.Join(saver.OutputPath, fmt.Sprintf(
  499. "%d_%d_before_%s.src", i, j, change.Change.From.TreeEntry.Hash.String()))
  500. goioutil.WriteFile(record.SrcBefore, []byte(s), 0666)
  501. bs, _ = change.After.Marshal()
  502. record.UastAfter = path.Join(saver.OutputPath, fmt.Sprintf(
  503. "%d_%d_after_%s.pb", i, j, change.Change.To.TreeEntry.Hash.String()))
  504. goioutil.WriteFile(record.UastAfter, bs, 0666)
  505. blob, _ = saver.repository.BlobObject(change.Change.To.TreeEntry.Hash)
  506. s, _ = (&object.File{Blob: *blob}).Contents()
  507. record.SrcAfter = path.Join(saver.OutputPath, fmt.Sprintf(
  508. "%d_%d_after_%s.src", i, j, change.Change.To.TreeEntry.Hash.String()))
  509. goioutil.WriteFile(record.SrcAfter, []byte(s), 0666)
  510. fileNames = append(fileNames, record)
  511. }
  512. }
  513. return fileNames
  514. }
  515. func (saver *ChangesSaver) serializeText(result []*pb.UASTChange, writer io.Writer) {
  516. for _, sc := range result {
  517. kv := [...]string{
  518. "file: " + sc.FileName,
  519. "src0: " + sc.SrcBefore, "src1: " + sc.SrcAfter,
  520. "uast0: " + sc.UastBefore, "uast1: " + sc.UastAfter,
  521. }
  522. fmt.Fprintf(writer, " - {%s}\n", strings.Join(kv[:], ", "))
  523. }
  524. }
  525. func (saver *ChangesSaver) serializeBinary(result []*pb.UASTChange, writer io.Writer) error {
  526. message := pb.UASTChangesSaverResults{Changes: result}
  527. serialized, err := proto.Marshal(&message)
  528. if err != nil {
  529. return err
  530. }
  531. writer.Write(serialized)
  532. return nil
  533. }
  534. func init() {
  535. core.Registry.Register(&Extractor{})
  536. core.Registry.Register(&Changes{})
  537. core.Registry.Register(&ChangesSaver{})
  538. }