-
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.
Parse dhall, convert to json, flatten to plain object
- Loading branch information
Showing
7 changed files
with
127 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "dotcrypt" | ||
|
||
config = Dotcrypt::Dhall.load(File.read(ARGV.first)) | ||
|
||
# puts(JSON.pretty_generate(config)) | ||
|
||
puts(JSON.pretty_generate(Dotcrypt::Flattener.call(config))) | ||
|
||
# puts(Dotcrypt::Compiler.(config)) |
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,19 @@ | ||
let some_variable = "some value" | ||
in { | ||
some_string = some_variable, | ||
some_record = { | ||
some_string = some_variable, | ||
some_naturals = [1,2,3], | ||
some_doubles = [1.2, 2.3], | ||
some_strings = ["value1", "value2"], | ||
-- some_records = [ | ||
-- { | ||
-- key = "value" | ||
-- }, | ||
-- { | ||
-- key = "value" | ||
-- } | ||
-- ], | ||
some_nil = None | ||
} | ||
} |
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dotcrypt::Dhall | ||
using Dotcrypt::Dhall::AsHash | ||
|
||
class << self | ||
def load(dhall) | ||
Dhall.load(dhall).sync.as_hash | ||
end | ||
end | ||
end |
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dotcrypt::Dhall::AsHash | ||
refine Dhall::Record do | ||
def as_hash(*) | ||
to_h.transform_values(&:as_hash) | ||
end | ||
end | ||
|
||
refine Dhall::List do | ||
def as_hash(*) | ||
to_a.map(&:as_hash) | ||
end | ||
end | ||
|
||
refine Dhall::Text do | ||
def as_hash(*) | ||
to_s | ||
end | ||
end | ||
|
||
refine Dhall::Natural do | ||
def as_hash(*) | ||
to_i | ||
end | ||
end | ||
|
||
refine Dhall::Double do | ||
def as_hash(*) | ||
to_f | ||
end | ||
end | ||
|
||
refine Dhall::Builtins::None do | ||
def as_hash(*) | ||
nil | ||
end | ||
end | ||
end |
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
class Dotcrypt::Flattener | ||
SCALARS = [String, Numeric, NilClass].freeze | ||
|
||
def self.call(...) = new(...).call | ||
|
||
def initialize(config, prefixes: [], accumulator: {}, separator: "_") | ||
@config = config | ||
@prefixes = prefixes | ||
@accumulator = accumulator | ||
@separator = separator | ||
end | ||
|
||
def call | ||
case config | ||
when *SCALARS then save(@config) | ||
when Hash then config.each { |k, v| self.class.call(v, prefixes: prefixes + [k], accumulator:, separator:) } | ||
when Array then flatten_array | ||
else raise TypeError, "unknown type: #{config.class}" | ||
end | ||
|
||
accumulator | ||
end | ||
|
||
private | ||
|
||
attr_reader :config, :prefixes, :accumulator, :separator | ||
|
||
def flatten_array | ||
raise TypeError, "only arrays of scalars are allowed, given: #{config}" unless config.all? { scalar?(_1) } | ||
|
||
save(config.to_json) | ||
end | ||
|
||
def scalar?(value) = SCALARS.any? { value.is_a?(_1) } | ||
|
||
def path = @prefixes.join(@separator) | ||
|
||
def save(value) = @accumulator[path] = value | ||
end |