service.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package parser
  2. import (
  3. "errors"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/emicklei/proto"
  8. )
  9. type (
  10. // Services is a slice of Service.
  11. Services []Service
  12. // Service describes the rpc service, which is the relevant
  13. // content after the translation of the proto file
  14. Service struct {
  15. *proto.Service
  16. RPC []*RPC
  17. }
  18. )
  19. func (s Services) validate(filename string, multipleOpt ...bool) error {
  20. if len(s) == 0 {
  21. return errors.New("rpc service not found")
  22. }
  23. var multiple bool
  24. for _, c := range multipleOpt {
  25. multiple = c
  26. }
  27. if !multiple && len(s) > 1 {
  28. return errors.New("only one service expected")
  29. }
  30. name := filepath.Base(filename)
  31. for _, service := range s {
  32. for _, rpc := range service.RPC {
  33. if strings.Contains(rpc.RequestType, ".") {
  34. return fmt.Errorf("line %v:%v, request type must defined in %s",
  35. rpc.Position.Line,
  36. rpc.Position.Column, name)
  37. }
  38. if strings.Contains(rpc.ReturnsType, ".") {
  39. return fmt.Errorf("line %v:%v, returns type must defined in %s",
  40. rpc.Position.Line,
  41. rpc.Position.Column, name)
  42. }
  43. }
  44. }
  45. return nil
  46. }