-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also expose component builder to outside Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
- Loading branch information
Showing
14 changed files
with
482 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2024 Richard Kosegi | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package pipeline | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/rkosegi/yaml-toolkit/patch" | ||
"github.com/rkosegi/yaml-toolkit/path" | ||
"github.com/rkosegi/yaml-toolkit/props" | ||
"gopkg.in/yaml.v3" | ||
"reflect" | ||
) | ||
|
||
type PathSyntax string | ||
|
||
const ( | ||
// PathSyntaxProps is using syntax of Java properties | ||
PathSyntaxProps = PathSyntax("properties") | ||
// PathSyntaxJsonPointer syntax according to https://datatracker.ietf.org/doc/html/rfc6901 | ||
PathSyntaxJsonPointer = PathSyntax("rfc6901") | ||
) | ||
|
||
var pathParsersMap = map[PathSyntax]path.Parser{ | ||
PathSyntaxProps: props.NewPathParser(), | ||
PathSyntaxJsonPointer: patch.NewPathParser(), | ||
} | ||
|
||
type UniversalPath struct { | ||
Value path.Path | ||
Syntax PathSyntax | ||
} | ||
|
||
func (u *UniversalPath) UnmarshalYAML(node *yaml.Node) error { | ||
var ( | ||
val string | ||
ok bool | ||
x interface{} | ||
) | ||
u.Syntax = PathSyntaxProps | ||
|
||
switch node.Kind { | ||
case yaml.ScalarNode: | ||
val = node.Value | ||
case yaml.MappingNode: | ||
m := make(map[string]interface{}) | ||
// TODO: how to provoke error from this call? | ||
_ = node.Decode(&m) | ||
if x, ok = m["value"]; !ok { | ||
return errors.New("missing 'value' field under path") | ||
} | ||
if val, ok = x.(string); !ok { | ||
return fmt.Errorf("'value' field is not a string (actual type: %v)", reflect.TypeOf(x)) | ||
} | ||
if syn, ok := m["syntax"]; ok { | ||
u.Syntax = PathSyntax(syn.(string)) | ||
} | ||
default: | ||
return fmt.Errorf("node kind is not supported: %v", node.Kind) | ||
} | ||
|
||
if pf, ok := pathParsersMap[u.Syntax]; ok { | ||
p, err := pf.Parse(val) | ||
if err != nil { | ||
return err | ||
} | ||
u.Value = p | ||
} else { | ||
return fmt.Errorf("unrecognized path syntax: %s", u.Syntax) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.