You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

  1. // +build ignore
  2. package main
  3. import (
  4. "bytes"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "regexp"
  10. "strings"
  11. )
  12. // This script replaces most `[]byte` with `data.Bytes` in a `.pb.go` file.
  13. // It was written before we realized we could use `gogo/protobuf` to achieve
  14. // this more natively. So it's here for safe keeping in case we ever need to
  15. // abandon `gogo/protobuf`.
  16. func main() {
  17. bytePattern := regexp.MustCompile("[[][]]byte")
  18. const oldPath = "types/types.pb.go"
  19. const tmpPath = "types/types.pb.new"
  20. content, err := ioutil.ReadFile(oldPath)
  21. if err != nil {
  22. panic("cannot read " + oldPath)
  23. os.Exit(1)
  24. }
  25. lines := bytes.Split(content, []byte("\n"))
  26. outFile, _ := os.Create(tmpPath)
  27. wroteImport := false
  28. for _, line_bytes := range lines {
  29. line := string(line_bytes)
  30. gotPackageLine := strings.HasPrefix(line, "package ")
  31. writeImportTime := strings.HasPrefix(line, "import ")
  32. containsDescriptor := strings.Contains(line, "Descriptor")
  33. containsByteArray := strings.Contains(line, "[]byte")
  34. if containsByteArray && !containsDescriptor {
  35. line = string(bytePattern.ReplaceAll([]byte(line), []byte("data.Bytes")))
  36. }
  37. if writeImportTime && !wroteImport {
  38. wroteImport = true
  39. fmt.Fprintf(outFile, "import \"github.com/tendermint/go-amino/data\"\n")
  40. }
  41. if gotPackageLine {
  42. fmt.Fprintf(outFile, "%s\n", "//nolint: gas")
  43. }
  44. fmt.Fprintf(outFile, "%s\n", line)
  45. }
  46. outFile.Close()
  47. os.Remove(oldPath)
  48. os.Rename(tmpPath, oldPath)
  49. exec.Command("goimports", "-w", oldPath)
  50. }