From 1dbf4543d41952f4002a1ff5a004a4f2ba349d53 Mon Sep 17 00:00:00 2001 From: Richard Kosegi Date: Thu, 4 Jul 2024 21:19:41 +0200 Subject: [PATCH] Path: Add new interfaces Also expose component builder to outside Signed-off-by: Richard Kosegi --- path/builder.go | 17 +++++++++++++---- path/builder_test.go | 2 +- path/types.go | 12 ++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/path/builder.go b/path/builder.go index d125f66..0b041e9 100644 --- a/path/builder.go +++ b/path/builder.go @@ -32,6 +32,16 @@ func (b *builder) Build() Path { } func (b *builder) Append(opts ...AppendOpt) Builder { + b.components = append(b.components, *buildComponent(opts...)) + return b +} + +// NewBuilder creates new Builder +func NewBuilder() Builder { + return &builder{} +} + +func buildComponent(opts ...AppendOpt) *component { if len(opts) == 0 { panic("no append option provided by caller") } @@ -39,10 +49,9 @@ func (b *builder) Append(opts ...AppendOpt) Builder { for _, opt := range opts { opt(c) } - b.components = append(b.components, *c) - return b + return c } -func NewBuilder() Builder { - return &builder{} +func BuildComponent(opts ...AppendOpt) Component { + return buildComponent(opts...) } diff --git a/path/builder_test.go b/path/builder_test.go index add27e8..b44062c 100644 --- a/path/builder_test.go +++ b/path/builder_test.go @@ -49,7 +49,7 @@ func TestBuilderAppendNoOption(t *testing.T) { defer func() { recover() }() - NewBuilder().Append() + BuildComponent() assert.Fail(t, "should not be here") } diff --git a/path/types.go b/path/types.go index 3d321d3..34dfd38 100644 --- a/path/types.go +++ b/path/types.go @@ -63,3 +63,15 @@ type Path interface { } type AppendOpt func(*component) + +// Parser interface is implemented by different Path syntax parsers. +type Parser interface { + // Parse parses source string into Path + Parse(string) (Path, error) +} + +// Serializer is interface that allows to serialize Path into lexical form +type Serializer interface { + // Serialize serializes path into lexical representation + Serialize(Path) string +}