Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/variadic modifiable #3

Merged
merged 4 commits into from
Jun 22, 2024
Merged
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
28 changes: 28 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Go Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22

- name: Install dependencies
run: go mod tidy

- name: Run tests
run: go test ./... -v
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Managing dependency with Submodule

![Go Test](https://github.com/submodule-org/submodule.go/actions/workflows/go-test.yml/badge.svg)
![common case](common-case.png "Common case")

Does the demonstrated diagram look familiar to you? A lot of applications will look just like so.
Expand Down
9 changes: 6 additions & 3 deletions modifiable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "reflect"
// That'll help the submodule to be way easier to reconfigure (for example, sharing loggers) without missing the default settings
type ModifiableSubmodule[T any] interface {
Submodule[T]
Append(submodule Retrievable)
Append(submodule ...Retrievable)
Reset()
}

Expand Down Expand Up @@ -60,8 +60,11 @@ func (m *modifiableSubmodule[T]) retrieve(s Scope) (any, error) {
return m.submodule.retrieve(s)
}

func (m *modifiableSubmodule[T]) Append(submodule Retrievable) {
m.modifiers = append(m.modifiers, submodule)
func (m *modifiableSubmodule[T]) Append(submodule ...Retrievable) {
if len(submodule) == 0 {
return
}
m.modifiers = append(m.modifiers, submodule...)
}

func (m *modifiableSubmodule[T]) Reset() {
Expand Down
26 changes: 26 additions & 0 deletions submodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,33 @@ func TestModuleFunction(t *testing.T) {
require.Nil(t, e)
require.Equal(t, 8, z)
})
t.Run("use variadic modifiable submodule", func(t *testing.T) {
type (
num1 int
num2 int
)
x := submodule.Value[num1](0)
y := submodule.Value[num2](0)

s := submodule.CreateScope()

type nums struct {
x num1
y num2
}

m := submodule.MakeModifiable[nums](func(x num1, y num2) nums {
return nums{x, y}
}, x, y)

m.Append(
submodule.Value[num1](1),
submodule.Value[num2](2),
)
rs, e := m.SafeResolveWith(s)
require.Nil(t, e)
require.Equal(t, nums{1, 2}, rs)
})
}

type Counter struct {
Expand Down
Loading