Skip to content

Commit

Permalink
config google wire
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmesquitta committed Aug 15, 2024
1 parent 0893657 commit b9ba860
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 65 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: default install test lint update build copy clear zip
.PHONY: default install gen test lint update build copy clear zip

default: dev

Expand All @@ -8,6 +8,8 @@ run:
@go run ./cmd/cli/main.go
install:
@go mod download
gen:
@go generate ./...
test:
@go test -count=1 -v ./...
lint:
Expand Down
37 changes: 3 additions & 34 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
package cli
package main

import (
"log/slog"

"github.com/danielmesquitta/openfinance/internal/config"
"github.com/danielmesquitta/openfinance/internal/domain/usecase"
"github.com/danielmesquitta/openfinance/internal/pkg/validator"
"github.com/danielmesquitta/openfinance/internal/provider/companyapi/brasilapi"
"github.com/danielmesquitta/openfinance/internal/provider/gpt/openai"
"github.com/danielmesquitta/openfinance/internal/provider/openfinance/meupluggyapi"
"github.com/danielmesquitta/openfinance/internal/provider/sheet/notionapi"
)
import "github.com/danielmesquitta/openfinance/internal/app/cli"

func main() {
val := validator.NewValidator()
env := config.NewEnv(val)
companyAPIProvider := brasilapi.NewClient()
gptProvider := openai.NewOpenAIClient(env)
sheetProvider := notionapi.NewClient(env)
openFinanceAPIProvider := meupluggyapi.NewClient(env)

u := usecase.NewSyncAll(
val,
env,
companyAPIProvider,
gptProvider,
sheetProvider,
openFinanceAPIProvider,
)

err := u.Execute(usecase.SyncAllDTO{})
if err != nil {
if err := cli.Run(); err != nil {
panic(err)
}

slog.Info(
"SyncAll executed successfully",
)
}
3 changes: 3 additions & 0 deletions gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

//go:generate go run -mod=mod github.com/google/wire/cmd/wire
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-playground/locales v0.14.1
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.22.0
github.com/google/wire v0.6.0
github.com/sashabaranov/go-openai v1.28.1
github.com/spf13/viper v1.19.0
)
Expand Down
54 changes: 54 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/wire v0.6.0 h1:HBkoIh4BdSxoyo9PveV8giw7ZsaBOvzWKfcg/6MrVwI=
github.com/google/wire v0.6.0/go.mod h1:F4QhpQ9EDIdJ1Mbop/NZBRB+5yrR6qg3BnctaoUk6NA=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down Expand Up @@ -69,18 +73,68 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
17 changes: 17 additions & 0 deletions internal/app/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

import (
"github.com/danielmesquitta/openfinance/internal/app"
"github.com/danielmesquitta/openfinance/internal/domain/usecase"
)

func Run() error {
syncAllUseCase := app.NewSyncAllUseCase()

err := syncAllUseCase.Execute(usecase.SyncDTO{})
if err != nil {
return err
}

return nil
}
28 changes: 2 additions & 26 deletions internal/app/lambda/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,14 @@ import (
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/danielmesquitta/openfinance/internal/config"
"github.com/danielmesquitta/openfinance/internal/app"
"github.com/danielmesquitta/openfinance/internal/domain/usecase"
"github.com/danielmesquitta/openfinance/internal/pkg/validator"
"github.com/danielmesquitta/openfinance/internal/provider/companyapi/brasilapi"
"github.com/danielmesquitta/openfinance/internal/provider/gpt/openai"
"github.com/danielmesquitta/openfinance/internal/provider/openfinance/meupluggyapi"
"github.com/danielmesquitta/openfinance/internal/provider/sheet/notionapi"
)

func Handler(
_ events.APIGatewayProxyRequest,
) (events.APIGatewayProxyResponse, error) {
val := validator.NewValidator()
env := config.NewEnv(val)
companyAPIProvider := brasilapi.NewClient()
gptProvider := openai.NewOpenAIClient(env)
sheetProvider := notionapi.NewClient(env)
openFinanceAPIProvider := meupluggyapi.NewClient(env)

syncOneUseCase := usecase.NewSyncOne(
val,
companyAPIProvider,
gptProvider,
sheetProvider,
openFinanceAPIProvider,
)

syncAllUseCase := usecase.NewSyncAll(
val,
env,
syncOneUseCase,
)
syncAllUseCase := app.NewSyncAllUseCase()

err := syncAllUseCase.Execute(usecase.SyncDTO{})
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions internal/app/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build wireinject
// +build wireinject

package app

import (
"github.com/danielmesquitta/openfinance/internal/config"
"github.com/danielmesquitta/openfinance/internal/domain/usecase"
"github.com/danielmesquitta/openfinance/internal/pkg/validator"
"github.com/danielmesquitta/openfinance/internal/provider/companyapi"
"github.com/danielmesquitta/openfinance/internal/provider/companyapi/brasilapi"
"github.com/danielmesquitta/openfinance/internal/provider/gpt"
"github.com/danielmesquitta/openfinance/internal/provider/gpt/openai"
"github.com/danielmesquitta/openfinance/internal/provider/openfinance"
"github.com/danielmesquitta/openfinance/internal/provider/openfinance/meupluggyapi"
"github.com/danielmesquitta/openfinance/internal/provider/sheet"
"github.com/danielmesquitta/openfinance/internal/provider/sheet/notionapi"
"github.com/google/wire"
)

func NewSyncAllUseCase() *usecase.SyncAll {
wire.Build(
validator.NewValidator,
config.NewEnv,

wire.Bind(new(companyapi.APIProvider), new(*brasilapi.Client)),
brasilapi.NewClient,

wire.Bind(new(gpt.Provider), new(*openai.OpenAIClient)),
openai.NewOpenAIClient,

wire.Bind(new(sheet.Provider), new(*notionapi.Client)),
notionapi.NewClient,

wire.Bind(new(openfinance.APIProvider), new(*meupluggyapi.Client)),
meupluggyapi.NewClient,

usecase.NewSyncOne,
usecase.NewSyncAll,
)

return &usecase.SyncAll{}
}
31 changes: 31 additions & 0 deletions internal/app/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/domain/usecase/sync_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
type SyncOne struct {
val *validator.Validator
companyAPIProvider companyapi.APIProvider
gptProvider gpt.GPTProvider
gptProvider gpt.Provider
sheetProvider sheet.Provider
openFinanceAPIProvider openfinance.APIProvider
}

func NewSyncOne(
val *validator.Validator,
companyAPIProvider companyapi.APIProvider,
gptProvider gpt.GPTProvider,
gptProvider gpt.Provider,
sheetProvider sheet.Provider,
openFinanceAPIProvider openfinance.APIProvider,
) *SyncOne {
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/gpt/gpt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package gpt

type GPTProvider interface {
type Provider interface {
CreateChatCompletion(message string) (string, error)
}
2 changes: 1 addition & 1 deletion internal/provider/gpt/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ func (o *OpenAIClient) CreateChatCompletion(message string) (string, error) {
return resp.Choices[0].Message.Content, nil
}

var _ gpt.GPTProvider = (*OpenAIClient)(nil)
var _ gpt.Provider = (*OpenAIClient)(nil)

0 comments on commit b9ba860

Please sign in to comment.