uast.go 19 KB

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