Skip to content

Commit

Permalink
Merge pull request #28 from go-kod/examples/config
Browse files Browse the repository at this point in the history
chore: add config & shorturl example
  • Loading branch information
sysulq authored Feb 19, 2024
2 parents 058ff4b + eda9f82 commit 52cbc1b
Show file tree
Hide file tree
Showing 18 changed files with 463 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ jobs:
fail-fast: false
matrix:
version: ["1.21"]
services:
redis:
image: redis
ports:
- 6379:6379
steps:
- name: Check out repository
uses: actions/checkout@v4
Expand Down
11 changes: 5 additions & 6 deletions cmd/kod/internal/struct2interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func makeFile(file string) (*makeInterfaceFile, error) {

func Struct2Interface(cmd *cobra.Command, dir string) error {
var mapDirPath = make(map[string]*makeInterfaceFile)
lo.Must0(filepath.WalkDir(dir, func(path string, d fs.DirEntry, _ error) error {
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, _ error) error {
if d == nil || d.IsDir() {
return nil
}
Expand All @@ -331,10 +331,6 @@ func Struct2Interface(cmd *cobra.Command, dir string) error {
return fmt.Errorf("makeFile error: %s", err.Error())
}

if result == nil {
return nil
}

if obj, ok := mapDirPath[filepath.Dir(path)+result.PkgName]; ok {

obj.AllImports = append(obj.AllImports, result.AllImports...)
Expand All @@ -358,7 +354,10 @@ func Struct2Interface(cmd *cobra.Command, dir string) error {
}

return nil
}))
})
if err != nil {
return err
}

return createFile(cmd, mapDirPath)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/kod/internal/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func Watch(watcher Watcher, dir string, callback func()) {
continue
}

if !strings.HasSuffix(filepath.Base(event.Name), ".go") {
continue
}

log.Println("modified file:", event.Name)
callback()
case err, ok := <-watcher.Errors():
Expand Down
24 changes: 23 additions & 1 deletion cmd/kod/internal/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,29 @@ func TestWatcherNormal(t *testing.T) {

events := make(chan fsnotify.Event, 2)
events <- fsnotify.Event{
Name: "test",
Name: "test.go",
Op: fsnotify.Write,
}

w.EXPECT().Events().Return(events).AnyTimes()
w.EXPECT().Errors().Return(nil).AnyTimes()

time.AfterFunc(time.Second, func() {
close(events)
})

Watch(w, ".", func() {})
}

func TestWatcherNonGofile(t *testing.T) {
t.Parallel()

w := NewMockWatcher(gomock.NewController(t))
w.EXPECT().Add(".").Return(nil)

events := make(chan fsnotify.Event, 2)
events <- fsnotify.Event{
Name: "test.txt",
Op: fsnotify.Write,
}

Expand Down
13 changes: 13 additions & 0 deletions examples/config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
useconfig
===

This is an example of how to read configuration from a file.

## Usage

```bash
KOD_CONFIG=kod.toml go run .
```

THis will read the configuration from `kod.toml` and print it to the console.
Kod also supports other formats like `json` and `yaml`.
2 changes: 2 additions & 0 deletions examples/config/kod.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
["github.com/go-kod/kod/Main"]
name = "config"
47 changes: 47 additions & 0 deletions examples/config/kod_gen.go

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

23 changes: 23 additions & 0 deletions examples/config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"context"

"github.com/go-kod/kod"
)

type app struct {
kod.Implements[kod.Main]
kod.WithConfig[config]
}

type config struct {
Name string
}

func main() {
kod.Run(context.Background(), func(ctx context.Context, app *app) error {
app.L(ctx).Info("Hello, " + app.Config().Name)
return nil
})
}
18 changes: 18 additions & 0 deletions examples/config/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"context"
"testing"

"github.com/go-kod/kod"
"github.com/stretchr/testify/assert"
)

// TestApp tests the app component.
func TestApp(t *testing.T) {
t.Parallel()

kod.RunTest(t, func(ctx context.Context, comp *app) {
assert.Equal(t, "config", comp.Config().Name)
}, kod.WithConfigFile("./kod.toml"))
}
3 changes: 2 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kod/kod/examples

go 1.21.7
go 1.21.0

replace github.com/go-kod/kod => ../

Expand All @@ -14,6 +14,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dominikbraun/graph v0.23.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
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/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
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.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
2 changes: 2 additions & 0 deletions examples/shorturl/kod.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
["github.com/go-kod/kod/examples/shorturl/Component"]
redisconfig = { addr = "localhost:6379" }
94 changes: 94 additions & 0 deletions examples/shorturl/kod_gen.go

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

15 changes: 15 additions & 0 deletions examples/shorturl/kod_gen_interface.go

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

Loading

0 comments on commit 52cbc1b

Please sign in to comment.