Skip to content

Commit

Permalink
docutil: Add Transport.Has() for inclusion judgements
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellLuo committed Mar 6, 2023
1 parent cc99725 commit c726dba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 49 deletions.
46 changes: 8 additions & 38 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (g *Generator) Generate(srcFilename, interfaceName string) (files []*genera
return nil, err
}

newSpec, transports, err := httpparser.Parse(data, g.opts.SnakeCase)
newSpec, transport, err := httpparser.Parse(data, g.opts.SnakeCase)
if err != nil {
return nil, err
}
Expand All @@ -114,54 +114,31 @@ func (g *Generator) Generate(srcFilename, interfaceName string) (files []*genera
}
files = append(files, epFile)

switch mergeTransports(transports) {
case docutil.TransportHTTP:
httpFiles, err := g.generateHTTP(data, spec)
if err != nil {
return files, err
}
files = append(files, httpFiles...)

case docutil.TransportGRPC:
grpcFiles, err := g.generateGRPC(data)
if err != nil {
return files, err
}
files = append(files, grpcFiles...)

case docutil.TransportEvent:
eventFiles, err := g.generateEvent(data, spec)
if err != nil {
return files, err
}
files = append(files, eventFiles...)

case docutil.TransportCron:
cronFiles, err := g.generateCron(data, spec)
if err != nil {
return files, err
}
files = append(files, cronFiles...)

case docutil.TransportAll:
if transport.Has(docutil.TransportHTTP) {
httpFiles, err := g.generateHTTP(data, spec)
if err != nil {
return files, err
}
files = append(files, httpFiles...)
}

if transport.Has(docutil.TransportGRPC) {
grpcFiles, err := g.generateGRPC(data)
if err != nil {
return files, err
}
files = append(files, grpcFiles...)
}

if transport.Has(docutil.TransportEvent) {
eventFiles, err := g.generateEvent(data, spec)
if err != nil {
return files, err
}
files = append(files, eventFiles...)
}

if transport.Has(docutil.TransportCron) {
cronFiles, err := g.generateCron(data, spec)
if err != nil {
return files, err
Expand Down Expand Up @@ -375,13 +352,6 @@ func (g *Generator) getPkgInfo(dir string) *generator.PkgInfo {
return pkgInfo
}

func mergeTransports(transports []docutil.Transport) (result docutil.Transport) {
for _, t := range transports {
result = result | t
}
return result
}

func ensureDir(path string) error {
return os.MkdirAll(path, 0755)
}
20 changes: 10 additions & 10 deletions gen/http/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ var (
rePathVarName = regexp.MustCompile(`{(\w+)}`)
)

func Parse(data *ifacetool.Data, snakeCase bool) (*spec.Specification, []docutil.Transport, error) {
func Parse(data *ifacetool.Data, snakeCase bool) (*spec.Specification, docutil.Transport, error) {
anno, err := annotation.ParseInterfaceAnnotation(data.InterfaceDoc)
if err != nil {
return nil, nil, err
return nil, 0, err
}

s := &spec.Specification{
Metadata: anno.Metadata,
}

var (
transports []docutil.Transport
opBuilder = &OpBuilder{
transport docutil.Transport
opBuilder = &OpBuilder{
snakeCase: snakeCase,
aliases: anno.Aliases,
}
Expand All @@ -46,14 +46,14 @@ func Parse(data *ifacetool.Data, snakeCase bool) (*spec.Specification, []docutil
doc := docutil.Doc(m.Doc).JoinComments()
m.Doc = doc // Replace the original doc with joined doc.

transport := doc.Transport()
if transport == 0 {
t := doc.Transport()
if t == 0 {
// Empty transport indicates that there are no annotations.
continue
}
transports = append(transports, transport)
transport |= t

if transport&docutil.TransportHTTP != docutil.TransportHTTP {
if !t.Has(docutil.TransportHTTP) {
// Add operations for generating endpoint code for gRPC.
op := spec.NewOperation(m.Name, m.Name, annotation.GetDescriptionFromDoc(m.Doc))
for _, arg := range m.Params {
Expand All @@ -65,13 +65,13 @@ func Parse(data *ifacetool.Data, snakeCase bool) (*spec.Specification, []docutil

ops, err := opBuilder.Build(m)
if err != nil {
return nil, nil, err
return nil, 0, err
}

s.Operations = append(s.Operations, ops...)
}

return s, transports, nil
return s, transport, nil
}

type OpBuilder struct {
Expand Down
5 changes: 4 additions & 1 deletion gen/util/docutil/docutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import (

type Transport int

func (t Transport) Has(o Transport) bool {
return t&o == o
}

const (
TransportHTTP Transport = 0b0001
TransportGRPC Transport = 0b0010
TransportEvent Transport = 0b0100
TransportCron Transport = 0b1000
TransportAll Transport = 0b1111
)

type Doc []string
Expand Down

0 comments on commit c726dba

Please sign in to comment.