Skip to content

Commit

Permalink
Merge pull request #22 from kinde-oss/ev/other_config_marshalling
Browse files Browse the repository at this point in the history
Marshalling other config as well for backwards compatibility
  • Loading branch information
evgenyk authored Nov 15, 2024
2 parents 1696ece + 745234d commit 2452f2a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
44 changes: 28 additions & 16 deletions projectBundler/projectBundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ type (
}

KindeEnvironment[TWorkflowSettings, TPageSettings any] struct {
Workflows []KindeWorkflow[TWorkflowSettings] `json:"workflows"`
Pages []KindePage[TPageSettings] `json:"pages"`
Workflows []KindeWorkflow[TWorkflowSettings] `json:"workflows"`
Pages []KindePage[TPageSettings] `json:"pages"`
discoveryOptions DiscoveryOptions[TWorkflowSettings, TPageSettings]
}

KindeProject[TWorkflowSettings, TPageSettings any] struct {
Expand All @@ -44,7 +45,9 @@ type (
}

DiscoveryOptions[TWorkflowSettings, TPageSettings any] struct {
StartFolder string
StartFolder string
OnWorkflowDiscovered func(bundle *bundler.BundlerResult[TWorkflowSettings])
OnPageDiscovered func(bundle *bundler.BundlerResult[TPageSettings])
}

ProjectBundler[TWorkflowSettings, TPageSettings any] interface {
Expand Down Expand Up @@ -85,7 +88,7 @@ func maybeAddWorkflow[TWorkflowSettings, TPageSettings any](ctx context.Context,
WorkflowRootDirectory: rootDirectory,
EntryPoints: []string{file},
}
discoveredWorkflow.bundleAndIntrospect(ctx)
discoveredWorkflow.bundleAndIntrospect(ctx, kw.discoveryOptions.OnWorkflowDiscovered)
kw.Workflows = append(kw.Workflows, discoveredWorkflow)
}
}
Expand Down Expand Up @@ -116,23 +119,28 @@ func maybeAddPage[TWorkflowSettings, TPageSettings any](ctx context.Context, fil
RootDirectory: rootDirectory,
EntryPoints: []string{file},
}
discoveredPage.bundleAndIntrospect(ctx)
discoveredPage.bundleAndIntrospect(ctx, kw.discoveryOptions.OnPageDiscovered)
kw.Pages = append(kw.Pages, discoveredPage)
}
}

// Discover implements ProjectBundler.
func (p *projectBundler[TWorkflowSettings, TPageSettings]) Discover(ctx context.Context) (*KindeProject[TWorkflowSettings, TPageSettings], error) {
result := &KindeProject[TWorkflowSettings, TPageSettings]{}
err := result.discoverKindeRoot(p.options.StartFolder)
project := &KindeProject[TWorkflowSettings, TPageSettings]{
Environment: KindeEnvironment[TWorkflowSettings, TPageSettings]{
discoveryOptions: p.options,
},
}

err := project.discoverKindeRoot(p.options.StartFolder)
if err != nil {
return nil, err
}

result.Environment.discoverWorkflows(ctx, filepath.Join(result.Configuration.AbsLocation, result.Configuration.RootDir))
result.Environment.discoverPages(ctx, filepath.Join(result.Configuration.AbsLocation, result.Configuration.RootDir))
project.Environment.discoverWorkflows(ctx, filepath.Join(project.Configuration.AbsLocation, project.Configuration.RootDir))
project.Environment.discoverPages(ctx, filepath.Join(project.Configuration.AbsLocation, project.Configuration.RootDir))

return result, nil
return project, nil
}

func (kp *KindeProject[TWorkflowSettings, TPageSettings]) discoverKindeRoot(startFolder string) error {
Expand Down Expand Up @@ -185,19 +193,23 @@ func (*KindeProject[TWorkflowSettings, TPageSettings]) readProjectConfiguration(
return result, nil
}

func (kw *KindeWorkflow[TSettings]) bundleAndIntrospect(ctx context.Context) {
func (kw *KindeWorkflow[TSettings]) bundleAndIntrospect(ctx context.Context, onDiscovered func(bundle *bundler.BundlerResult[TSettings])) {
workflowBuilder := bundler.NewWorkflowBundler(bundler.BundlerOptions[TSettings]{
WorkingFolder: kw.WorkflowRootDirectory,
EntryPoints: kw.EntryPoints,
WorkingFolder: kw.WorkflowRootDirectory,
EntryPoints: kw.EntryPoints,
IntrospectionExport: "workflowSettings",
OnDiscovered: onDiscovered,
})
bundlerResult := workflowBuilder.Bundle(ctx)
kw.Bundle = bundlerResult
}

func (kw *KindePage[TSettings]) bundleAndIntrospect(ctx context.Context) {
func (kw *KindePage[TSettings]) bundleAndIntrospect(ctx context.Context, onDiscovered func(bundle *bundler.BundlerResult[TSettings])) {
workflowBuilder := bundler.NewWorkflowBundler(bundler.BundlerOptions[TSettings]{
WorkingFolder: kw.RootDirectory,
EntryPoints: kw.EntryPoints,
WorkingFolder: kw.RootDirectory,
EntryPoints: kw.EntryPoints,
IntrospectionExport: "pageSettings",
OnDiscovered: onDiscovered,
})
bundlerResult := workflowBuilder.Bundle(ctx)
kw.Bundle = bundlerResult
Expand Down
13 changes: 13 additions & 0 deletions projectBundler/projectBundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"testing"

bundler "github.com/kinde-oss/workflows-runtime/workflowBundler"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,8 +19,16 @@ func Test_ProjectBunler(t *testing.T) {
type pageSettings struct {
}

onWorkflowDiscoveredCalled := 0
onPageDiscoveredCalled := 0
projectBundler := NewProjectBundler(DiscoveryOptions[workflowSettings, pageSettings]{
StartFolder: somePathInsideProject,
OnWorkflowDiscovered: func(bundle *bundler.BundlerResult[workflowSettings]) {
onWorkflowDiscoveredCalled++
},
OnPageDiscovered: func(bundle *bundler.BundlerResult[pageSettings]) {
onPageDiscoveredCalled++
},
})

kindeProject, discoveryError := projectBundler.Discover(context.Background())
Expand All @@ -29,6 +38,10 @@ func Test_ProjectBunler(t *testing.T) {
if !assert.Nil(discoveryError) {
t.FailNow()
}

assert.Equal(3, onWorkflowDiscoveredCalled)
assert.Equal(2, onPageDiscoveredCalled)

assert.Equal("2024-12-09", kindeProject.Configuration.Version)
assert.Equal("kindeSrc", kindeProject.Configuration.RootDir)
assert.Equal(3, len(kindeProject.Environment.Workflows))
Expand Down
9 changes: 3 additions & 6 deletions workflowBundler/workflowBundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type (
bundlerContext string

WorkflowSettings[TSettings any] struct {
Bindings map[string]runtimesRegistry.BindingSettings `json:"bindings"`
Additional TSettings
Bindings map[string]runtimesRegistry.BindingSettings `json:"bindings"`
Other TSettings `json:"other"`
}

BundledContent[TSettings any] struct {
Expand Down Expand Up @@ -50,9 +50,6 @@ type (
)

func NewWorkflowBundler[TSettings any](options BundlerOptions[TSettings]) WorkflowBundler[TSettings] {
if options.IntrospectionExport == "" {
options.IntrospectionExport = "workflowSettings"
}
return &builder[TSettings]{
bundleOptions: options,
}
Expand Down Expand Up @@ -188,7 +185,7 @@ func (settings *WorkflowSettings[TSettings]) UnmarshalJSON(data []byte) error {
set := new(TSettings)
err = json.Unmarshal(data, set)

settings.Additional = *set
settings.Other = *set

return err

Expand Down
9 changes: 5 additions & 4 deletions workflowBundler/workflowBundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func Test_WorkflowBundler(t *testing.T) {
pluginSetupWasCalled := false

workflowBuilder := NewWorkflowBundler[workflowSettings](BundlerOptions[workflowSettings]{
WorkingFolder: workflowPath,
EntryPoints: []string{"tokensWorkflow.ts"},
WorkingFolder: workflowPath,
EntryPoints: []string{"tokensWorkflow.ts"},
IntrospectionExport: "workflowSettings",
OnDiscovered: func(bundle *BundlerResult[workflowSettings]) {
bundle.Errors = append(bundle.Errors, "ID is required")
},
Expand All @@ -41,7 +42,7 @@ func Test_WorkflowBundler(t *testing.T) {
assert.True(pluginSetupWasCalled, "plugin setup was not called")
assert.Equal(bundlerResult.Errors[0], "ID is required")
assert.NotEmpty(bundlerResult.Content.Source)
assert.Equal("tokenGen", bundlerResult.Content.Settings.Additional.ID)
assert.Equal("onTokenGeneration", bundlerResult.Content.Settings.Additional.Trigger)
assert.Equal("tokenGen", bundlerResult.Content.Settings.Other.ID)
assert.Equal("onTokenGeneration", bundlerResult.Content.Settings.Other.Trigger)
assert.NotEmpty(bundlerResult.Content.BundleHash)
}

0 comments on commit 2452f2a

Please sign in to comment.