Skip to content

Commit

Permalink
add OptionalReference
Browse files Browse the repository at this point in the history
  • Loading branch information
mkideal committed Aug 10, 2024
1 parent 19c8f62 commit be7ad2d
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,19 @@ func (c *BaseComponent[T]) Setup(container Container, config Config) error {
type Reference[T any] struct {
component T
uuid string
optional bool
}

// Ref creates a reference to a component with the given UUID.
func Ref[T any](uuid string) Reference[T] {
return Reference[T]{uuid: uuid}
}

// OptionalRef creates an optional reference to a component with the given UUID.
func OptionalRef[T any](uuid string) Reference[T] {
return Reference[T]{uuid: uuid, optional: true}
}

// UUID returns the UUID of the referenced component.
func (r Reference[T]) UUID() string {
return r.uuid
}

// Component returns the referenced component.
// If optional is true and the uuid is empty, it returns nil.
func (r Reference[T]) Component() T {
return r.component
}
Expand All @@ -166,9 +159,6 @@ func (r *Reference[T]) UnmarshalJSON(data []byte) error {

// Resolve resolves the reference for the component.
func (r *Reference[T]) Resolve(container Container) error {
if r.optional && r.uuid == "" {
return nil
}
com := container.GetComponent(r.uuid)
if com == nil {
return fmt.Errorf("component %q not found", r.uuid)
Expand All @@ -180,6 +170,27 @@ func (r *Reference[T]) Resolve(container Container) error {
return fmt.Errorf("unexpected component %q type: %T", r.uuid, com)
}

// OptionalReference represents an optional reference to another component.
// If the UUID is empty, the reference is ignored, and Component returns nil.
type OptionalReference[T any] struct {
Reference[T]
}

// OptionalRef creates an optional reference to a component with the given UUID.
func OptionalRef[T any](uuid string) OptionalReference[T] {
return OptionalReference[T]{
Reference: Reference[T]{uuid: uuid},
}
}

// Resolve resolves the reference for the component.
func (r *OptionalReference[T]) Resolve(container Container) error {
if r.uuid == "" {
return nil
}
return r.Reference.Resolve(container)
}

// BaseComponentWithRefs provides a basic implementation of the Component interface with references.
type BaseComponentWithRefs[T, R any] struct {
BaseComponent[T]
Expand Down

0 comments on commit be7ad2d

Please sign in to comment.