Skip to content

Commit

Permalink
refactor: refactor project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtARTs36 committed Aug 14, 2024
1 parent 8ecf81c commit 4f56386
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 86 deletions.
51 changes: 27 additions & 24 deletions internal/srv_collector.go → internal/collector/srv_collector.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package internal
package collector

import (
"fmt"
"strings"

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

"google.golang.org/protobuf/compiler/protogen"
)

Expand All @@ -15,10 +18,10 @@ func NewSrvCollector() *SrvCollector {
}

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

type handlerFileNames struct {
Expand All @@ -27,12 +30,12 @@ type handlerFileNames struct {
withoutDomain string
}

func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*Services, error) {
services := &Services{
Services: []*Service{},
func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*entity.Services, error) {
services := &entity.Services{
Services: []*entity.Service{},
}

apiImportPkg := APIImportPackage{
apiImportPkg := entity.APIImportPackage{
FullName: string(file.GoImportPath),
Alias: string(file.GoPackageName),
}
Expand All @@ -49,7 +52,7 @@ func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*Services
for _, service := range file.Services {
srvPkg := c.generatePackageName(service, opts)

srv := &Service{
srv := &entity.Service{
PackageName: srvPkg,
Name: c.generateServiceName(service, opts),
Domain: strings.TrimSuffix(service.GoName, "Service"),
Expand All @@ -61,7 +64,7 @@ func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*Services

handlersByFiles := map[string]handlerFileNames{}

srv.Handlers = map[string]*Handler{}
srv.Handlers = map[string]*entity.Handler{}

for _, method := range service.Methods {
names := c.generateHandlerFilename(method, srv, srvPkg, opts)
Expand All @@ -78,18 +81,18 @@ func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*Services

handlersByFiles[names.selected] = names

inputMsg := &Message{
inputMsg := &entity.Message{
Name: string(method.Input.Desc.Name()),
Properties: MessageProperties{
All: make([]*MessageProperty, 0),
Required: make([]*MessageProperty, 0),
Properties: entity.MessageProperties{
All: make([]*entity.MessageProperty, 0),
Required: make([]*entity.MessageProperty, 0),
},
}

for _, field := range method.Input.Fields {
prop := &MessageProperty{
prop := &entity.MessageProperty{
GoName: field.GoName,
Type: createValType(field.Desc.Kind()),
Type: entity.CreateValType(field.Desc.Kind()),
Required: !field.Desc.HasOptionalKeyword(),
Optional: field.Desc.HasOptionalKeyword(),
}
Expand All @@ -102,7 +105,7 @@ func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*Services

c.setMessageValidateableFields(inputMsg, opts)

handler := &Handler{
handler := &entity.Handler{
Filename: names.selected,
MethodName: method.GoName,
InputMsgStructName: string(method.Input.Desc.Name()),
Expand All @@ -121,15 +124,15 @@ func (c *SrvCollector) Collect(file *protogen.File, opts CollectOpts) (*Services
}

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

return strings.TrimSuffix(strings.ToLower(srv.GoName), "service")
}

func (*SrvCollector) generateServiceName(srv *protogen.Service, opts CollectOpts) string {
if opts.SrvNaming == SrvNamingAsIs {
if opts.SrvNaming == options.SrvNamingAsIs {
return srv.GoName
}

Expand All @@ -138,7 +141,7 @@ func (*SrvCollector) generateServiceName(srv *protogen.Service, opts CollectOpts

func (*SrvCollector) generateHandlerFilename(
method *protogen.Method,
srv *Service,
srv *entity.Service,
pkg string,
opts CollectOpts,
) handlerFileNames {
Expand All @@ -155,7 +158,7 @@ func (*SrvCollector) generateHandlerFilename(
),
}

if opts.HandlerFileNaming == HandlerFileNamingAsIs {
if opts.HandlerFileNaming == options.HandlerFileNamingAsIs {
names.selected = names.asIs
} else {
names.selected = names.withoutDomain
Expand All @@ -164,8 +167,8 @@ func (*SrvCollector) generateHandlerFilename(
return names
}

func (*SrvCollector) setMessageValidateableFields(msg *Message, opts CollectOpts) {
if opts.RequestValidator.Type != RequestValidatorTypeNo {
func (*SrvCollector) setMessageValidateableFields(msg *entity.Message, opts CollectOpts) {
if opts.RequestValidator.Type != entity.RequestValidatorTypeNo {
msg.Properties.Validateable = msg.Properties.Required
}
}
17 changes: 6 additions & 11 deletions internal/handler.go → internal/entity/handler.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package internal
package entity

import (
"strings"
)

type HandlerFileNaming string

const (
HandlerFileNamingAsIs HandlerFileNaming = "as_is"
HandlerFileNamingWithoutDomain HandlerFileNaming = "without_domain"
"github.com/artarts36/protoc-gen-go-srv-handler/internal/options"
)

type Message struct {
Expand Down Expand Up @@ -68,10 +63,10 @@ func (h *Handler) TestFileName() string {
return strings.Replace(h.Filename, ".go", "_test.go", 1)
}

func CreateHandlerFileNaming(val string) HandlerFileNaming {
if val == string(HandlerFileNamingWithoutDomain) {
return HandlerFileNamingWithoutDomain
func CreateHandlerFileNaming(val string) options.HandlerFileNaming {
if val == string(options.HandlerFileNamingWithoutDomain) {
return options.HandlerFileNamingWithoutDomain
}

return HandlerFileNamingAsIs
return options.HandlerFileNamingAsIs
}
2 changes: 1 addition & 1 deletion internal/req.go → internal/entity/req.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package entity

type RequestValidatorType string

Expand Down
15 changes: 1 addition & 14 deletions internal/srv.go → internal/entity/srv.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
package internal
package entity

type SrvNaming string

const (
SrvNamingAsIs SrvNaming = "as_is"
SrvNamingJustService SrvNaming = "just_service"
)

func CreateSrvNaming(val string) SrvNaming {
if val == string(SrvNamingJustService) {
return SrvNamingJustService
}

return SrvNamingAsIs
}

type Services struct {
Services []*Service
}
Expand Down
4 changes: 2 additions & 2 deletions internal/type.go → internal/entity/type.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package entity

import "google.golang.org/protobuf/reflect/protoreflect"

Expand All @@ -12,7 +12,7 @@ const (
ValTypeBool ValType = iota
)

func createValType(t protoreflect.Kind) ValType {
func CreateValType(t protoreflect.Kind) ValType {
switch t { //nolint: exhaustive // not need
case protoreflect.StringKind:
return ValTypeString
Expand Down
8 changes: 8 additions & 0 deletions internal/options/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package options

type HandlerFileNaming string

const (
HandlerFileNamingAsIs HandlerFileNaming = "as_is"
HandlerFileNamingWithoutDomain HandlerFileNaming = "without_domain"
)
2 changes: 1 addition & 1 deletion internal/pkg.go → internal/options/pkg.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package options

type PkgNaming string

Expand Down
16 changes: 16 additions & 0 deletions internal/options/srv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package options

type SrvNaming string

const (
SrvNamingAsIs SrvNaming = "as_is"
SrvNamingJustService SrvNaming = "just_service"
)

func CreateSrvNaming(val string) SrvNaming {
if val == string(SrvNamingJustService) {
return SrvNamingJustService
}

return SrvNamingAsIs
}
16 changes: 9 additions & 7 deletions internal/renderer.go → internal/renderer/renderer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package internal
package renderer

import (
"html/template"
"io"

"github.com/artarts36/protoc-gen-go-srv-handler/internal/entity"

"github.com/artarts36/protoc-gen-go-srv-handler/templates"
)

Expand All @@ -26,31 +28,31 @@ func NewRenderer() (*Renderer, error) {
return rend, nil
}

func (r *Renderer) RenderService(w io.Writer, srv *Service) error {
func (r *Renderer) RenderService(w io.Writer, srv *entity.Service) error {
return r.templates.service.ExecuteTemplate(w, "service.template", map[string]interface{}{
"Service": srv,
})
}

func (r *Renderer) RenderServiceTest(w io.Writer, srv *Service) error {
func (r *Renderer) RenderServiceTest(w io.Writer, srv *entity.Service) error {
return r.templates.service.ExecuteTemplate(w, "service_test.template", map[string]interface{}{
"Service": srv,
})
}

func (r *Renderer) RenderHandler(w io.Writer, srv *Service, hand *Handler, params RenderHandlerParams) error {
func (r *Renderer) RenderHandler(w io.Writer, hand *entity.Handler, params RenderHandlerParams) error {
return r.templates.service.ExecuteTemplate(w, "handler.template", map[string]interface{}{
"Service": srv,
"Service": hand.Service,
"Handler": hand,
"Params": params,
})
}

type RenderHandlerParams struct {
RequestValidator RequestValidator
RequestValidator entity.RequestValidator
}

func (r *Renderer) RenderHandlerTest(w io.Writer, hand *Handler, params RenderHandlerParams) error {
func (r *Renderer) RenderHandlerTest(w io.Writer, hand *entity.Handler, params RenderHandlerParams) error {
return r.templates.service.ExecuteTemplate(w, "handler_test.template", map[string]interface{}{
"Service": hand.Service,
"Handler": hand,
Expand Down
Loading

0 comments on commit 4f56386

Please sign in to comment.