Skip to content

Commit

Permalink
Parse dhall, convert to json, flatten to plain object
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Jan 1, 2023
1 parent 7871d24 commit 05e2793
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 7 deletions.
7 changes: 0 additions & 7 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,5 @@
require "bundler/setup"
require "dotcrypt"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
13 changes: 13 additions & 0 deletions bin/dhall
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))
19 changes: 19 additions & 0 deletions example.dhall
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
}
}
4 changes: 4 additions & 0 deletions lib/dotcrypt.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

require "json"

require "zeitwerk"

require "dhall"

loader = Zeitwerk::Loader.for_gem

loader.setup
Expand Down
11 changes: 11 additions & 0 deletions lib/dotcrypt/dhall.rb
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
39 changes: 39 additions & 0 deletions lib/dotcrypt/dhall/as_hash.rb
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
41 changes: 41 additions & 0 deletions lib/dotcrypt/flattener.rb
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

0 comments on commit 05e2793

Please sign in to comment.