parser.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package parser
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "github.com/tal-tech/go-zero/core/lang"
  6. "github.com/tal-tech/go-zero/tools/goctl/util/console"
  7. )
  8. func Transfer(proto, target string, externalImport []*Import, console console.Console) (*PbAst, error) {
  9. messageM := make(map[string]lang.PlaceholderType)
  10. enumM := make(map[string]*Enum)
  11. protoAst, err := parseProto(proto, messageM, enumM)
  12. if err != nil {
  13. return nil, err
  14. }
  15. for _, item := range externalImport {
  16. err = checkImport(item.OriginalProtoPath)
  17. if err != nil {
  18. return nil, err
  19. }
  20. innerAst, err := parseProto(item.OriginalProtoPath, protoAst.Message, protoAst.Enum)
  21. if err != nil {
  22. return nil, err
  23. }
  24. for k, v := range innerAst.Message {
  25. protoAst.Message[k] = v
  26. }
  27. for k, v := range innerAst.Enum {
  28. protoAst.Enum[k] = v
  29. }
  30. }
  31. protoAst.Import = externalImport
  32. protoAst.PbSrc = filepath.Join(target, strings.TrimSuffix(filepath.Base(proto), ".proto")+".pb.go")
  33. return transfer(protoAst, console)
  34. }
  35. func transfer(proto *Proto, console console.Console) (*PbAst, error) {
  36. parser := MustNewAstParser(proto, console)
  37. parse, err := parser.Parse()
  38. if err != nil {
  39. return nil, err
  40. }
  41. return parse, nil
  42. }