Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Feb 14, 2024
1 parent 57ee452 commit 60c1a4a
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var (
// See: https://regex101.com/r/lIuuep/1
ReferenceRegex = regexp.MustCompile(`\${(?P<reference>[\w-]+(?:\:[\w-]+)*)}`)

ErrUndefinedReference = fmt.Errorf("undefined reference")
ErrUndefinedReference = fmt.Errorf("undefined reference")
ErrReferenceSourceIsNil = fmt.Errorf("reference source is nil")
)

// Reference is a reference to a value with a different path.
Expand All @@ -35,29 +36,18 @@ func (ref Reference) Name() string {

type ResolvedReference struct {
Reference
// TargetValue is the value to which the TargetPath points to
// This can be [data.NilValue]. In that case there is no target
// value but a target reference.
// TargetValue is the value to which the TargetPath points to.
// If TargetReference is not nil, this value must be [data.NilValue].
TargetValue data.Value
// TargetReference is non-nil if the Reference points to another [Reference]
// If the Reference just points to a scalar value, this will be nil.
TargetReference *Reference
}

// ReferenceParser is responsible for discovering and resolving references.
type ReferenceParser struct {
source ReferenceSourceWalker
}

type ReferenceSourceWalker interface {
WalkValues(func(path data.Path, value data.Value) error) error
}

type ReferenceSourceGetter interface {
GetPath(path data.Path) (data.Value, error)
}

var ErrReferenceSourceIsNil = fmt.Errorf("source is nil")

func ParseReferences(source ReferenceSourceWalker) ([]Reference, error) {
if source == nil {
return nil, ErrReferenceSourceIsNil
Expand All @@ -82,6 +72,10 @@ func ParseReferences(source ReferenceSourceWalker) ([]Reference, error) {
return references, nil
}

type ReferenceSourceGetter interface {
GetPath(path data.Path) (data.Value, error)
}

func ResolveReferences(references []Reference, resolveSource ReferenceSourceGetter) ([]ResolvedReference, error) {
if resolveSource == nil {
return nil, ErrReferenceSourceIsNil
Expand Down

0 comments on commit 60c1a4a

Please sign in to comment.