Skip to content

Commit

Permalink
refactor: split collector fill properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtARTs36 committed Aug 14, 2024
1 parent 4f56386 commit 4338833
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
13 changes: 13 additions & 0 deletions internal/collector/opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package collector

import (
"github.com/artarts36/protoc-gen-go-srv-handler/internal/entity"
"github.com/artarts36/protoc-gen-go-srv-handler/internal/options"
)

type CollectOpts struct {
SrvNaming options.SrvNaming
PkgNaming options.PkgNaming
HandlerFileNaming options.HandlerFileNaming
RequestValidator entity.RequestValidator
}
38 changes: 17 additions & 21 deletions internal/collector/srv_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ func NewSrvCollector() *SrvCollector {
return &SrvCollector{}
}

type CollectOpts struct {
SrvNaming options.SrvNaming
PkgNaming options.PkgNaming
HandlerFileNaming options.HandlerFileNaming
RequestValidator entity.RequestValidator
}

type handlerFileNames struct {
selected string
asIs string
Expand Down Expand Up @@ -89,20 +82,7 @@ func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*entity.S
},
}

for _, field := range method.Input.Fields {
prop := &entity.MessageProperty{
GoName: field.GoName,
Type: entity.CreateValType(field.Desc.Kind()),
Required: !field.Desc.HasOptionalKeyword(),
Optional: field.Desc.HasOptionalKeyword(),
}

inputMsg.Properties.All = append(inputMsg.Properties.All, prop)
if prop.Required {
inputMsg.Properties.Required = append(inputMsg.Properties.Required, prop)
}
}

c.fillProperties(inputMsg, method.Input.Fields)
c.setMessageValidateableFields(inputMsg, opts)

handler := &entity.Handler{
Expand All @@ -123,6 +103,22 @@ func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*entity.S
return services, nil
}

func (*SrvCollector) fillProperties(msg *entity.Message, fields []*protogen.Field) {
for _, field := range fields {
prop := &entity.MessageProperty{
GoName: field.GoName,
Type: entity.CreateValType(field.Desc.Kind()),
Required: !field.Desc.HasOptionalKeyword(),
Optional: field.Desc.HasOptionalKeyword(),
}

msg.Properties.All = append(msg.Properties.All, prop)
if prop.Required {
msg.Properties.Required = append(msg.Properties.Required, prop)
}
}
}

func (*SrvCollector) generatePackageName(srv *protogen.Service, opts CollectOpts) string {
if opts.PkgNaming == options.PkgNamingAsIs {
return strings.ToLower(srv.GoName)
Expand Down

0 comments on commit 4338833

Please sign in to comment.