Skip to content

Commit

Permalink
refactor: utilized a consistent commenting style for better code docu…
Browse files Browse the repository at this point in the history
…mentation (#26)
  • Loading branch information
siyul-park authored Dec 1, 2023
1 parent b24f298 commit c3734fb
Show file tree
Hide file tree
Showing 43 changed files with 534 additions and 569 deletions.
4 changes: 2 additions & 2 deletions cmd/resource/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestBuilder_Build(t *testing.T) {
spec := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
Expand All @@ -40,7 +40,7 @@ func TestBuilder_Build(t *testing.T) {

builder := NewBuilder().
Scheme(s).
Namespace(scheme.NamespaceDefault).
Namespace(scheme.DefaultNamespace).
FS(fsys).
Filename(filename)

Expand Down
2 changes: 1 addition & 1 deletion cmd/resource/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *SpecCodec) Decode(data any) (scheme.Spec, error) {
if c.namespace != "" {
unstructured.SetNamespace(c.namespace)
} else {
unstructured.SetNamespace(scheme.NamespaceDefault)
unstructured.SetNamespace(scheme.DefaultNamespace)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/uniflow/apply/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestExecute(t *testing.T) {
spec := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/uniflow/start/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestExecute(t *testing.T) {
spec := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
Expand Down
16 changes: 7 additions & 9 deletions pkg/hook/builder.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package hook

type (
// Builder builds a new Hooks.
Builder []func(*Hook) error
)
// Builder builds a new Hooks.
type Builder []func(*Hook) error

// NewBuilder returns a new HooksBuilder.
func NewBuilder(funcs ...func(*Hook) error) Builder {
return Builder(funcs)
}

// AddToHooks adds all registered hook to s.
func (b *Builder) AddToHooks(h *Hook) error {
for _, f := range *b {
// AddToHooks adds all registered hooks to h.
func (b Builder) AddToHooks(h *Hook) error {
for _, f := range b {
if err := f(h); err != nil {
return err
}
}
return nil
}

// Register adds one or more hook.
// Register adds one or more hooks.
func (b *Builder) Register(funcs ...func(*Hook) error) {
*b = append(*b, funcs...)
}

// Build returns a new Hooks containing the registered hooks.
func (b *Builder) Build() (*Hook, error) {
func (b Builder) Build() (*Hook, error) {
h := New()
if err := b.AddToHooks(h); err != nil {
return nil, err
Expand Down
24 changes: 11 additions & 13 deletions pkg/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,38 @@ import (
"github.com/siyul-park/uniflow/pkg/symbol"
)

type (
// Hook is a collection of hook functions.
Hook struct {
loadHooks []symbol.LoadHook
unloadHooks []symbol.UnloadHook
mu sync.RWMutex
}
)
// Hook is a collection of hook functions for loading and unloading nodes.
type Hook struct {
loadHooks []symbol.LoadHook
unloadHooks []symbol.UnloadHook
mu sync.RWMutex
}

var _ symbol.LoadHook = &Hook{}
var _ symbol.UnloadHook = &Hook{}

// New returns a new Hooks.
// New creates a new Hook instance.
func New() *Hook {
return &Hook{}
}

// AddLoadHook adds a LoadHook.
// AddLoadHook adds a LoadHook function to the Hook.
func (h *Hook) AddLoadHook(hook symbol.LoadHook) {
h.mu.Lock()
defer h.mu.Unlock()

h.loadHooks = append(h.loadHooks, hook)
}

// AddUnloadHook adds a UnloadHook.
// AddUnloadHook adds an UnloadHook function to the Hook.
func (h *Hook) AddUnloadHook(hook symbol.UnloadHook) {
h.mu.Lock()
defer h.mu.Unlock()

h.unloadHooks = append(h.unloadHooks, hook)
}

// Load runs LoadHooks.
// Load executes LoadHooks on the provided node.
func (h *Hook) Load(n node.Node) error {
h.mu.RLock()
defer h.mu.RUnlock()
Expand All @@ -53,7 +51,7 @@ func (h *Hook) Load(n node.Node) error {
return nil
}

// Unload runs UnloadHooks.
// Unload executes UnloadHooks on the provided node.
func (h *Hook) Unload(n node.Node) error {
h.mu.RLock()
defer h.mu.RUnlock()
Expand Down
56 changes: 32 additions & 24 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,22 @@ import (
"github.com/siyul-park/uniflow/pkg/symbol"
)

type (
// Config represents the configuration for the Loader.
Config struct {
Namespace string // Namespace is the namespace used by the Loader.
Table *symbol.Table // Table is the symbol table for managing symbols.
Scheme *scheme.Scheme // Scheme is the scheme used by the Loader.
Storage *storage.Storage // Storage is the storage used by the Loader.
}
// Config represents the configuration for the Loader.
type Config struct {
Namespace string // Namespace is the namespace used by the Loader.
Table *symbol.Table // Table is the symbol table for managing symbols.
Scheme *scheme.Scheme // Scheme is the scheme used by the Loader.
Storage *storage.Storage // Storage is the storage used by the Loader.
}

// Loader loads scheme.Spec into the symbol.Table.
Loader struct {
namespace string
scheme *scheme.Scheme
table *symbol.Table
storage *storage.Storage
mu sync.RWMutex
}
)
// Loader loads scheme.Spec into the symbol.Table.
type Loader struct {
namespace string
scheme *scheme.Scheme
table *symbol.Table
storage *storage.Storage
mu sync.RWMutex
}

// New returns a new Loader.
func New(config Config) *Loader {
Expand All @@ -48,30 +46,34 @@ func New(config Config) *Loader {
}
}

// LoadOne loads a single scheme.Spec from the storage.Storage
// LoadOne loads a single scheme.Spec from the storage.Storage.
// It processes the specified ID and recursively loads linked scheme.Spec.
// If the loader is associated with a namespace, it uses that namespace.
// The loaded nodes are added to the symbol table for future reference.
func (ld *Loader) LoadOne(ctx context.Context, id ulid.ULID) (node.Node, error) {
ld.mu.Lock()
defer ld.mu.Unlock()

namespace := ld.namespace

queue := []any{id}

for len(queue) > 0 {
prev := queue
queue = nil

exists := map[any]bool{}

var filter *storage.Filter

for _, key := range prev {
if k, ok := key.(ulid.ULID); ok {
switch k := key.(type) {
case ulid.ULID:
exists[k] = false
filter = filter.Or(storage.Where[ulid.ULID](scheme.KeyID).EQ(k))
} else if k, ok := key.(string); ok {
case string:
exists[k] = false
filter = filter.Or(storage.Where[string](scheme.KeyName).EQ(k))
}
}

if namespace != "" {
filter = filter.And(storage.Where[string](scheme.KeyNamespace).EQ(namespace))
}
Expand Down Expand Up @@ -139,9 +141,15 @@ func (ld *Loader) LoadOne(ctx context.Context, id ulid.ULID) (node.Node, error)
}
}

// LoadAll loads all scheme.Spec from the storage.Storage
// LoadAll loads all scheme.Spec from the storage.Storage.
// It loads all available scheme.Spec and adds them to the symbol table for future reference.
// If the loader is associated with a namespace, it filters the loading based on that namespace.
func (ld *Loader) LoadAll(ctx context.Context) ([]node.Node, error) {
ld.mu.Lock()
defer ld.mu.Unlock()

var filter *storage.Filter

if ld.namespace != "" {
filter = filter.And(storage.Where[string](scheme.KeyNamespace).EQ(ld.namespace))
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestLoader_LoadOne(t *testing.T) {
spec1 := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
}
spec2 := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
Links: map[string][]scheme.PortLocation{
node.PortIO: {
{
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestLoader_LoadOne(t *testing.T) {
spec := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestLoader_LoadOne(t *testing.T) {
spec := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
Expand Down Expand Up @@ -187,12 +187,12 @@ func TestLoader_LoadAll(t *testing.T) {
spec1 := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
}
spec2 := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
Links: map[string][]scheme.PortLocation{
node.PortIO: {
{
Expand Down
18 changes: 9 additions & 9 deletions pkg/loader/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

type (
// ReconcilerConfig is a config for for the Reconciler.
// ReconcilerConfig holds the configuration for the Reconciler.
ReconcilerConfig struct {
Storage *storage.Storage
Loader *Loader
Filter *storage.Filter
Storage *storage.Storage // Storage is the storage used by the Reconciler.
Loader *Loader // Loader is used to load scheme.Spec into the symbol.Table.
Filter *storage.Filter // Filter is the filter for tracking changes to the scheme.Spec.
}

// Reconciler keeps up to date symbol.Table by tracking changes to the scheme.Spec.
// Reconciler keeps the symbol.Table up to date by tracking changes to scheme.Spec.
Reconciler struct {
storage *storage.Storage
loader *Loader
Expand All @@ -26,7 +26,7 @@ type (
}
)

// NewReconciler returns a new Reconciler.
// NewReconciler creates a new Reconciler with the given configuration.
func NewReconciler(config ReconcilerConfig) *Reconciler {
storage := config.Storage
loader := config.Loader
Expand All @@ -40,13 +40,13 @@ func NewReconciler(config ReconcilerConfig) *Reconciler {
}
}

// Watch starts to watch the changes.
// Watch starts watching for changes to scheme.Spec.
func (r *Reconciler) Watch(ctx context.Context) error {
_, err := r.watch(ctx)
return err
}

// Reconcile starts to reflects the changes.
// Reconcile reflects changes to scheme.Spec in the symbol.Table.
func (r *Reconciler) Reconcile(ctx context.Context) error {
stream, err := r.watch(ctx)
if err != nil {
Expand Down Expand Up @@ -76,7 +76,7 @@ func (r *Reconciler) Reconcile(ctx context.Context) error {
}
}

// Close closes the Reconciler.
// Close stops the Reconciler and closes the associated stream.
func (r *Reconciler) Close() error {
r.mu.Lock()
defer r.mu.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion pkg/loader/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestReconciler_Reconcile(t *testing.T) {
m := &scheme.SpecMeta{
ID: ulid.Make(),
Kind: kind,
Namespace: scheme.NamespaceDefault,
Namespace: scheme.DefaultNamespace,
}

codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/node/onetomany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestOneToManyNode_Send(t *testing.T) {
outPort.Link(out)

proc := process.New()
defer proc.Close()
defer proc.Exit()

inStream := in.Open(proc)
outStream := out.Open(proc)
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestOneToManyNode_Send(t *testing.T) {
errPort.Link(err)

proc := process.New()
defer proc.Close()
defer proc.Exit()

inStream := in.Open(proc)
errStream := err.Open(proc)
Expand Down
Loading

0 comments on commit c3734fb

Please sign in to comment.