Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions builder/hexagonal/hexagonal.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,7 @@ func (p *Project) GenerateRestPortService() error {

data := map[string]any{
"PackagePath": p.based.Project.PackagePath,
"ServiceName": p.RestPortService.Data.ServiceName,
"Methods": p.RestPortService.Data.Methods,
"Services": p.RestPortService.Data,
}
raw, err := libos.ExecuteTemplate(p.RestPortService.template, data)
if err != nil {
Expand Down Expand Up @@ -693,14 +692,14 @@ func (p *Project) GenerateRestResponse() error {

func (p *Project) GenerateRestHandlers() error {
var (
childsRouter []ChildRouterGroup
handlerDir = fmt.Sprintf("%s/internal/adapter/inbound/rest/router/v1/handler", p.based.Project.ProjectName)
presenterDir = fmt.Sprintf("%s/internal/adapter/inbound/rest/router/v1/presenter", p.based.Project.ProjectName)
routesGroupMap = make(map[string]RouterGroup)
domainMap = make(map[string]DataDomainModel)
serviceRegistryMap = make(map[string]bool)
serviceHandler DataRestPortService
servicesMap = make(map[string]ServiceParams)
childsRouter []ChildRouterGroup
handlerDir = fmt.Sprintf("%s/internal/adapter/inbound/rest/router/v1/handler", p.based.Project.ProjectName)
presenterDir = fmt.Sprintf("%s/internal/adapter/inbound/rest/router/v1/presenter", p.based.Project.ProjectName)
routesGroupMap = make(map[string]RouterGroup)
domainMap = make(map[string]DataDomainModel)
serviceRegistryMap = make(map[string]bool)
servicePortHandlerMap = make(map[string]DataRestPortService)
servicesMap = make(map[string]ServiceParams)
)

fmt.Printf(" %s%s\n", ui.LineOnProgress, handlerDir)
Expand Down Expand Up @@ -848,15 +847,23 @@ func (p *Project) GenerateRestHandlers() error {
})

// service handler
serviceHandler.Methods = append(serviceHandler.Methods, handlerService)
serviceHandler.ServiceName = serviceName
if _, exist = servicePortHandlerMap[serviceName]; !exist {
servicePortHandlerMap[serviceName] = DataRestPortService{
ServiceName: serviceName,
Methods: []PortServiceMethods{handlerService},
}
} else {
service := servicePortHandlerMap[serviceName]
service.Methods = append(service.Methods, handlerService)
servicePortHandlerMap[serviceName] = service
}

// service service
if _, exist = servicesMap[serviceName]; !exist {
servicesMap[serviceName] = ServiceParams{
PackagePath: p.based.Project.PackagePath,
ServiceName: serviceName,
Methods: serviceHandler.Methods,
Methods: []PortServiceMethods{handlerService},
}
} else {
service := servicesMap[serviceName]
Expand Down Expand Up @@ -903,7 +910,12 @@ func (p *Project) GenerateRestHandlers() error {
}

p.RoutesGroup = routesGroup
p.RestPortService.Data = serviceHandler

var portServices []DataRestPortService
for _, portService := range servicePortHandlerMap {
portServices = append(portServices, portService)
}
p.RestPortService.Data = portServices

var domainsModel []DataDomainModel
for _, dm := range domainMap {
Expand Down Expand Up @@ -1105,11 +1117,16 @@ func (p *Project) GenerateRestService() error {
}
}

var serviceMethods []PortServiceMethods
for _, drps := range p.RestPortService.Data {
serviceMethods = append(serviceMethods, drps.Methods...)
}

for _, service := range p.Service.Services {
data := map[string]any{
"PackagePath": p.based.Project.PackagePath,
"ServiceName": service.ServiceName,
"Methods": p.RestPortService.Data.Methods,
"Methods": serviceMethods,
}

raw, err := libos.ExecuteTemplate(p.Service.template, data)
Expand Down
6 changes: 6 additions & 0 deletions builder/hexagonal/model_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ func (h *HandlerData) Generate(method string, operation *openapi3.Operation) err
}
}

// use struct name if ref not empty
if content.Schema.Ref != "" {
sn := strings.ReplaceAll(content.Schema.Ref, "#/components/schemas/", "")
sBody.StructName = sn
}

for key, property := range content.Schema.Value.Properties {
if !h.HasBody {
h.HasBody = true
Expand Down
2 changes: 1 addition & 1 deletion builder/hexagonal/model_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type RestPortService struct {
dirpath string
filepath string
template []byte
Data DataRestPortService
Data []DataRestPortService
}

type DataRestPortService struct {
Expand Down
19 changes: 14 additions & 5 deletions shared/oas/oas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package liboas

import (
"fmt"
"strings"

"github.com/getkin/kin-openapi/openapi3"
)
Expand Down Expand Up @@ -36,8 +37,11 @@ func CreateSwaggerAnnotation(path string, method string, operation *openapi3.Ope
if operation.RequestBody != nil {
for contentType, mediaType := range operation.RequestBody.Value.Content {
annotation += fmt.Sprintf("// @Accept %s\n", contentType)
annotation += fmt.Sprintf("// @Param body body %s true \"Request body\"\n",
mediaType.Schema.Value.Type)
fragment := mediaType.Schema.RefPath().Fragment
if fragment != "" && strings.Contains(fragment, "/components/schemas/") {
refName := strings.ReplaceAll(fragment, "/components/schemas/", "")
annotation += fmt.Sprintf("// @Param body body presenter.%s true %q\n", refName, "Request body")
}
}
}

Expand All @@ -52,9 +56,14 @@ func CreateSwaggerAnnotation(path string, method string, operation *openapi3.Ope
}
}

responseModel := mediaType.Schema.Ref
if responseModel != "" && strings.Contains(responseModel, "#/components/schemas/") {
responseModel = strings.ReplaceAll(responseModel, "#/components/schemas/", "")
}

annotation += fmt.Sprintf("// @Success %s {object} %s \"%s\"\n",
statusCode,
mediaType.Schema.Ref, // Reference to the schema
responseModel, // Reference to the schema
description)
}
}
Expand Down Expand Up @@ -88,7 +97,7 @@ func OASDescriptionSwagger(doc *openapi3.T) (string, error) {
}
}

if doc.Servers != nil && len(doc.Servers) > 0 {
if len(doc.Servers) > 0 {
annotation += fmt.Sprintf("// @host %s\n", doc.Servers[0].URL)
}

Expand Down Expand Up @@ -135,7 +144,7 @@ func DataTypeToGo(dataType string) string {
return "float64"
case "boolean":
return "bool"
case "null", "nullable":
case "null", "nullable", "array":
return "any"
default:
return "any"
Expand Down
36 changes: 18 additions & 18 deletions shared/oas/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func ParseSchema(parentStruct, contentType string, schema *openapi3.Schema, igno

field := PropertyStruct{
Name: name,
Type: prop.Value.Type.Slice()[0],
Type: DataTypeToGo(prop.Value.Type.Slice()[0]),
Enum: enumFunc(),
Format: prop.Value.Format,
Tag: GetTag(contentType, nameLower),
Expand All @@ -112,10 +112,10 @@ func ParseSchema(parentStruct, contentType string, schema *openapi3.Schema, igno
return ResponseStruct{}, err
}

resFieldNameData := strings.ToLower(nestedStruct.Name)
if ignoreDataResponse && resFieldNameData == "data" {
return nestedStruct, nil
}
// resFieldNameData := strings.ToLower(nestedStruct.Name)
// if ignoreDataResponse && strings.ToLower(resFieldNameData) == "data" {
// return nestedStruct, nil
// }

return ResponseStruct{
Name: res.Name,
Expand Down Expand Up @@ -150,26 +150,26 @@ func ParseSchema(parentStruct, contentType string, schema *openapi3.Schema, igno
return ResponseStruct{}, err
}

resFieldNameData := strings.ToLower(name)
if ignoreDataResponse && resFieldNameData == "data" {
return ResponseStruct{
Name: res.Name,
Type: propType,
Children: &ResponseStruct{
Name: arrayStruct.Name,
Type: arrayStruct.Type,
Fields: arrayStruct.Fields,
},
}, nil
}
// resFieldNameData := strings.ToLower(name)
// if ignoreDataResponse && resFieldNameData == "data" {
// return ResponseStruct{
// Name: res.Name,
// Type: propType,
// Children: &ResponseStruct{
// Name: arrayStruct.Name,
// Type: arrayStruct.Type,
// Fields: arrayStruct.Fields,
// },
// }, nil
// }

return ResponseStruct{
Name: res.Name,
Type: res.Type,
Fields: []PropertyStruct{
{
Name: name,
Type: propType,
Type: DataTypeToGo(propType),
Tag: GetTag(contentType, nameLower),
Children: &ResponseStruct{
Name: arrayStruct.Name,
Expand Down
51 changes: 22 additions & 29 deletions shared/templates/hexagonal/cmd/bootstrap/app_repository.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,49 @@ func InitializeRepository() portregistry.Repository {
{{if .IsRedis}}
cacheRepository: redisadapter.New(
redisadapter.RedisOptions{
Addr: config.AppRepository.Cache.Redis.Addr,
Username: config.AppRepository.Cache.Redis.Username,
Password: config.AppRepository.Cache.Redis.Password,
DB: config.AppRepository.Cache.Redis.DB,
Addr: config.App.Cache.Redis.Addr,
Username: config.App.Cache.Redis.Username,
Password: config.App.Cache.Redis.Password,
DB: config.App.Cache.Redis.DB,
},
),
{{end}}

{{if .IsPSQL}}
psqlRepository: psqladapter.New(psqladapter.PSQLConfig{
Host: config.AppRepository.Datastore.PSQL.Host,
Port: config.AppRepository.Datastore.PSQL.Port,
Username: config.AppRepository.Datastore.PSQL.Username,
Password: config.AppRepository.Datastore.PSQL.Password,
DB: config.AppRepository.Datastore.PSQL.DB,
Host: config.App.Datastore.PSQL.Host,
Port: config.App.Datastore.PSQL.Port,
Username: config.App.Datastore.PSQL.Username,
Password: config.App.Datastore.PSQL.Password,
DB: config.App.Datastore.PSQL.DB,
}),
{{end}}

{{if .IsSQLite}}
sqliteRepository: sqliteadapter.New(sqliteadapter.SQLiteConfig{
Path: config.AppRepository.Datastore.SQLite.Path,
Mode: config.AppRepository.Datastore.SQLite.Mode, // Optional
Path: config.App.Datastore.SQLite.Path,
Mode: config.App.Datastore.SQLite.Mode, // Optional
}),
{{end}}

{{if .IsMySQL}}
mysqlRepository: mysqladapter.New(mysqladapter.MySQLConfig{
Host: config.AppRepository.Datastore.MySQL.Host,
Port: config.AppRepository.Datastore.MySQL.Port,
Username: config.AppRepository.Datastore.MySQL.Username,
Password: config.AppRepository.Datastore.MySQL.Password,
DB: config.AppRepository.Datastore.MySQL.DB,
Host: config.App.Datastore.MySQL.Host,
Port: config.App.Datastore.MySQL.Port,
Username: config.App.Datastore.MySQL.Username,
Password: config.App.Datastore.MySQL.Password,
DB: config.App.Datastore.MySQL.DB,
}),
{{end}}

{{if .IsMongoDB}}
mongoRepository: mongoadapter.New(mongoadapter.MongoConfig{
Host: config.AppRepository.Datastore.MongoDB.Host,
Port: config.AppRepository.Datastore.MongoDB.Port,
Username: config.AppRepository.Datastore.MongoDB.Username,
Password: config.AppRepository.Datastore.MongoDB.Password,
DB: config.AppRepository.Datastore.MongoDB.DB,
AuthDB: config.AppRepository.Datastore.MongoDB.AuthDB, // Optional
Host: config.App.Datastore.MongoDB.Host,
Port: config.App.Datastore.MongoDB.Port,
Username: config.App.Datastore.MongoDB.Username,
Password: config.App.Datastore.MongoDB.Password,
DB: config.App.Datastore.MongoDB.DB,
AuthDB: config.App.Datastore.MongoDB.AuthDB, // Optional
}),
{{end}}
}
Expand Down Expand Up @@ -150,10 +150,3 @@ func (a *AppRepository) MongoRepository() repository.MongoRepository {
return a.mongoRepository
}
{{end}}

{{- range .Repositories}}
func (a *AppRepository) Get{{.}}() repository.{{.}}{
return nil
}
{{- end}}

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"{{.PackagePath}}/internal/adapter/inbound/rest/router/v1/presenter"
)


{{- if .Annotation}} {{.Annotation}} {{- end}}
func(h *Handler) {{.HandlerName}}() func(c *fiber.Ctx) error {
{{- if .Annotation }}
{{ .Annotation }}
{{- end }} func(h *Handler) {{.HandlerName}}() func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
{{if .HasService}}
var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
)

//go:generate mockgen -destination=../../../../../shared/mock/service/service.go -package=mockservices -source=service.go
type {{.ServiceName}} interface {
{{- range .Methods}}
{{.MethodName}}(ctx context.Context {{range .Params}}, {{.Name}} {{.Type}} {{end}}) ({{range $index, $element := .ReturnTypes}}{{if $index}}, {{end}}{{$element.Type}}{{end}})
{{- end}}
}
{{- range .Services}}
type {{.ServiceName}} interface {
{{- range .Methods}}
{{.MethodName}}(ctx context.Context {{range .Params}}, {{.Name}} {{.Type}} {{end}}) ({{range $index, $element := .ReturnTypes}}{{if $index}}, {{end}}{{$element.Type}}{{end}})
{{- end}}
}
{{- end}}

Loading