Skip to content

Commit

Permalink
bean to request
Browse files Browse the repository at this point in the history
  • Loading branch information
snowmerak committed Jun 17, 2023
1 parent aa332d9 commit 9a29a24
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ option go_package = "model/proto/person";
추가로 `jetti run <cmd-name> <args>...`을 실행하여 커맨드 패키지에 인자를 전달할 수 있습니다.
사실상 `go run`과 동일합니다.

## bean
## request scope data

`bean` `context.Context``WithValue`를 편리하게 이용할 수 있게 해주는 기능입니다.
`request scope data` `context.Context``WithValue`를 편리하게 이용할 수 있게 해주는 기능입니다.

빈의 핵심은 동등, 혹은 하위 문맥에서 동일한 객체 인스턴스를 공유하는 것입니다.
`request scope data`핵심은 동등, 혹은 하위 문맥에서 동일한 데이터를 공유하는 것입니다.

### 예시

Expand All @@ -98,12 +98,12 @@ option go_package = "model/proto/person";
```go
package config

// jetti:bean redis postgres
// jetti:request redis postgres
type Config struct {
}
```

`jetti:bean` 주석을 통해 `redis``postgres` 빈을 등록했습니다.
`jetti:request` 주석을 통해 `redis``postgres` 빈을 등록했습니다.

이제 터미널에 `jetti generate`를 입력하면 `./lib/redis.context.go``./lib/postgres.context.go` 파일이 생성됩니다.

Expand Down Expand Up @@ -142,7 +142,7 @@ func GetRedis(ctx context.Context) (*Config, bool) {
}
```

이제 단일 컨텍스트를 생성한 후, `Push` 메서드를 통해 빈을 등록하고, `Get` 메서드를 통해 빈을 가져올 수 있습니다.
이제 단일 컨텍스트를 생성한 후, `Push` 메서드를 통해 데이터를 컨텍스트에 추가하고, `Get` 메서드를 통해 데이터를 가져올 수 있습니다.

## optional parameter

Expand Down
21 changes: 11 additions & 10 deletions internal/executor/check/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ type Bean struct {
Aliases []string
}

func HasBean(pkg *model.Package) ([]Bean, error) {
func HasBean(pkg *model.Package, directive string) ([]Bean, error) {
beans := []Bean(nil)
directive = "jetti:" + directive

for _, st := range pkg.Structs {
if strings.Contains(st.Doc, "jetti:bean") {
if strings.Contains(st.Doc, directive) {
bean := Bean{
Type: TypeStruct,
Name: st.Name,
}
split := strings.Split(st.Doc, "\n")
for _, line := range split {
if strings.Contains(line, "jetti:bean") {
bean.Aliases = append(bean.Aliases, strings.Split(strings.TrimSpace(strings.TrimPrefix(line, "jetti:bean")), " ")...)
if strings.Contains(line, directive) {
bean.Aliases = append(bean.Aliases, strings.Split(strings.TrimSpace(strings.TrimPrefix(line, directive)), " ")...)
beans = append(beans, bean)
break
}
Expand All @@ -33,15 +34,15 @@ func HasBean(pkg *model.Package) ([]Bean, error) {
}

for _, it := range pkg.Interfaces {
if strings.Contains(it.Doc, "jetti:bean") {
if strings.Contains(it.Doc, directive) {
bean := Bean{
Type: TypeInterface,
Name: it.Name,
}
split := strings.Split(it.Doc, "\n")
for _, line := range split {
if strings.Contains(line, "jetti:bean") {
bean.Aliases = append(bean.Aliases, strings.Split(strings.TrimSpace(strings.TrimPrefix(line, "jetti:bean")), " ")...)
if strings.Contains(line, directive) {
bean.Aliases = append(bean.Aliases, strings.Split(strings.TrimSpace(strings.TrimPrefix(line, directive)), " ")...)
beans = append(beans, bean)
break
}
Expand All @@ -50,15 +51,15 @@ func HasBean(pkg *model.Package) ([]Bean, error) {
}

for _, ali := range pkg.Aliases {
if strings.Contains(ali.Doc, "jetti:bean") {
if strings.Contains(ali.Doc, directive) {
bean := Bean{
Type: TypeAlias,
Name: ali.Name,
}
split := strings.Split(ali.Doc, "\n")
for _, line := range split {
if strings.Contains(line, "jetti:bean") {
bean.Aliases = append(bean.Aliases, strings.Split(strings.TrimSpace(strings.TrimPrefix(line, "jetti:bean")), " ")...)
if strings.Contains(line, directive) {
bean.Aliases = append(bean.Aliases, strings.Split(strings.TrimSpace(strings.TrimPrefix(line, directive)), " ")...)
beans = append(beans, bean)
break
}
Expand Down
7 changes: 7 additions & 0 deletions internal/executor/check/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package check

import "github.com/snowmerak/jetti/v2/lib/model"

func HasError(pkg *model.Package) ([]string, error) {

}
6 changes: 3 additions & 3 deletions internal/executor/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func Generate(root string) error {
return err
}

beans, err := check.HasBean(pkg)
requests, err := check.HasBean(pkg, generate.Request)
if err != nil {
return err
}

if len(beans) > 0 {
if err := generate.Bean(path, beans); err != nil {
if len(requests) > 0 {
if err := generate.RequestScopeData(path, requests); err != nil {
return err
}
log.Printf("generate bean: %s", relativePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"strings"
)

func Bean(path string, beans []check.Bean) error {
const Request = "request"

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

Expand Down

0 comments on commit 9a29a24

Please sign in to comment.