Skip to content

Commit

Permalink
add parse yml/json filters
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Jan 21, 2022
1 parent 5359d24 commit ec53ec2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
1.1.1 (01/20/2022)
------------------

* add mutex for namespace creation [87645e7](../../commit/87645e7)
* option to exclude CRDs from multi-instance/namespace inheritance [217639a](../../commit/217639a)
* add a re_contains filter for rgex string comparison [464b848](../../commit/464b848)
* fix spec for active_templates to allow it to be unset [a3a6cab](../../commit/a3a6cab)
* allow specifying namespace for projectmappings in values.yaml [554a43e](../../commit/554a43e)
* fix example [af961d1](../../commit/af961d1)

1.1.0 (01/04/2022)
------------------

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ ones:
| `indent: count` | Indents each line in the argument by count spaces |
| `nindent: count` | Adds a leading newline, then indents each line in the argument by count spaces |
| `stringify` | Converts argument to a string safe to use in yaml (escapes quotes and surrounds with the quote character) |
| `to_yaml` | Converts argument to a yaml representation |
| `to_json` | Converts argument to a json representation |
| `parse_yaml` | Parses yaml string into a structured representation |
| `to_yaml` | Converts object to a yaml representation |
| `parse_json` | Parses json string into a structured representation |
| `to_json` | Converts object to a json representation |
| `encode64` | The argument bas64 encoded |
| `decode64` | The argument bas64 decoded |
| `sha256` | The sha256 digest of the argument |
Expand Down
18 changes: 13 additions & 5 deletions lib/kubetruth/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,23 @@ def stringify(str)
str.to_s.to_json
end

def to_yaml(str, options = {})
def parse_yaml(str)
YAML.safe_load(str)
end

def to_yaml(obj, options = {})
options = {} unless options.is_a?(Hash)
result = str.to_yaml
result = obj.to_yaml
result = result[4..-1] if options['no_header']
result
end

def to_json(str)
str.to_json
def parse_json(str)
JSON.parse(str)
end

def to_json(obj)
obj.to_json
end

def sha256(data)
Expand Down Expand Up @@ -193,7 +201,7 @@ def typify(data, parser="json")
end

def merge(lhs_map, rhs_map)
lhs_map.merge(rhs_map)
lhs_map.merge(rhs_map || {})
end

REGEXP_FLAGS = {
Expand Down
31 changes: 31 additions & 0 deletions spec/kubetruth/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ module Kubetruth

end

describe "#parse_yaml" do

it "parses a yaml string" do
expect(parse_yaml("---\n- 1\n- 2\n")).to eq([1, 2])
expect(parse_yaml("foo: bar")).to eq({"foo" => "bar"})
end

end

describe "#to_yaml" do

it "produces a yaml string" do
Expand All @@ -143,6 +152,16 @@ module Kubetruth

end

describe "#parse_json" do

it "parses a json string" do
expect(parse_json("[1, 2]")).to eq([1, 2])
expect(parse_json('{"foo": "bar"}')).to eq({"foo" => "bar"})
end

end


describe "#to_json" do

it "produces a json string" do
Expand Down Expand Up @@ -385,6 +404,14 @@ module Kubetruth
expect(described_class.new("{{ m1 | merge: m2 | to_json }}").render(m1: m1, m2: m2)).to eq(m1.merge(m2).to_json)
end


it "handles nil rhs" do
m1 = {"x" => "y", "a" => "z"}
m2 = nil
expect(merge(m1, m2)).to eq(m1)
expect(described_class.new("{{ m1 | merge: m2 | to_json }}").render(m1: m1, m2: m2)).to eq(m1.to_json)
end

end

describe "#re_replace" do
Expand All @@ -401,6 +428,10 @@ module Kubetruth
expect(re_replace("FOO\nOO", "f.*", "X", "mi")).to eq("X")
end

it "handles backrefs" do
expect(re_replace("foobar", "(o+)b", "XX\\1YY")).to eq("fXXooYYar")
end

end

describe "#re_contains" do
Expand Down

0 comments on commit ec53ec2

Please sign in to comment.