Skip to content

Commit

Permalink
remove local package container interface
Browse files Browse the repository at this point in the history
  • Loading branch information
snowmerak committed Aug 20, 2023
1 parent 652d3aa commit 07ff157
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
2 changes: 1 addition & 1 deletion internal/executor/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func Generate(root string) error {

if len(beans) > 0 {
beanUpdated = true
if err := generate.Bean(path, beans); err != nil {
if err := generate.Bean(moduleName, path, beans); err != nil {
return err
}
log.Printf("generate bean: %s", relativePath)
Expand Down
90 changes: 47 additions & 43 deletions internal/executor/generate/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,19 @@ var beanInterfaceValue = []model.Interface{
"return",
},
},
{
Name: "MakeChild",
Return: []model.Field{
{
Name: "child",
Type: "Container",
},
},
},
},
},
}

var beanCopyFunction = model.Function{
Name: "CopyBeanContainer",
Params: []model.Field{
{
Name: "dst",
Type: "Container",
},
{
Name: "src",
Type: "Container",
},
},
Code: []string{
"for _, key := range src.Keys() {",
"value, ok := src.Get(key)",
"if !ok {",
"continue",
"}",
"dst.Set(key, value)",
"}",
},
}

func BeanContainer(root string) error {
genPath := filepath.Join(root, "gen", "bean")
if err := os.MkdirAll(genPath, os.ModePerm); err != nil {
Expand All @@ -122,6 +108,10 @@ func BeanContainer(root string) error {
{
Name: "Default",
Fields: []model.Field{
{
Name: "parent",
Type: "Container",
},
{
Name: "beans",
Type: "map[any]any",
Expand Down Expand Up @@ -154,6 +144,11 @@ func BeanContainer(root string) error {
"$RECEIVER$.lock.RLock()",
"value, ok = $RECEIVER$.beans[key]",
"$RECEIVER$.lock.RUnlock()",
"if !ok {",
"if $RECEIVER$.parent != nil {",
"value, ok = $RECEIVER$.parent.Get(key)",
"}",
"}",
"return",
},
},
Expand Down Expand Up @@ -207,6 +202,22 @@ func BeanContainer(root string) error {
"return",
},
},
{
Name: "MakeChild",
Return: []model.Field{
{
Name: "child",
Type: "Container",
},
},
Code: []string{
"child = &Default{",
"parent: $RECEIVER$,",
"beans: make(map[any]any),",
"}",
"return",
},
},
},
},
},
Expand All @@ -226,7 +237,6 @@ func BeanContainer(root string) error {
"return",
},
},
beanCopyFunction,
},
}

Expand All @@ -242,33 +252,24 @@ func BeanContainer(root string) error {
return nil
}

func Bean(path string, beans []check.Bean) error {
func Bean(moduleName string, path string, beans []check.Bean) error {
dir := filepath.Dir(path)
packageName := filepath.Base(dir)

if len(beans) == 0 {
return nil
}

{
ifceFilePath := MakeGeneratedFileName(dir, "bean", "interface")
ifcePkg := &model.Package{
Name: packageName,
Interfaces: beanInterfaceValue,
Functions: []model.Function{beanCopyFunction},
}
ifceData, err := generator.GenerateFile(ifcePkg)
if err != nil {
return err
}
if err := os.WriteFile(ifceFilePath, ifceData, os.ModePerm); err != nil {
return err
}
}
beanPackagePath := filepath.Join(moduleName, "gen", "bean")
beanPackagePath = filepath.ToSlash(beanPackagePath)

for _, bean := range beans {
for _, alias := range bean.Aliases {
alias = strings.ToUpper(alias[:1]) + alias[1:]
origin := alias
alias = strings.ToUpper(alias[:1])
if len(origin) > 1 {
alias += origin[1:]
}
filePath := MakeGeneratedFileName(dir, strings.ToLower(alias), "bean")
typ := bean.Name
switch bean.Type {
Expand All @@ -283,6 +284,9 @@ func Bean(path string, beans []check.Bean) error {
{
Path: "errors",
},
{
Path: beanPackagePath,
},
},
Aliases: []model.Alias{
{
Expand All @@ -303,7 +307,7 @@ func Bean(path string, beans []check.Bean) error {
Params: []model.Field{
{
Name: "beanContainer",
Type: "Container",
Type: "bean.Container",
},
{
Name: "value",
Expand All @@ -319,7 +323,7 @@ func Bean(path string, beans []check.Bean) error {
Params: []model.Field{
{
Name: "beanContainer",
Type: "Container",
Type: "bean.Container",
},
},
Return: []model.Field{
Expand Down

0 comments on commit 07ff157

Please sign in to comment.