Skip to content

Commit

Permalink
Add json serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Jan 2, 2023
1 parent 1b3b202 commit b810b69
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ Style/Documentation:
Style/SymbolArray:
EnforcedStyle: brackets

Style/WordArray:
EnforcedStyle: brackets

Style/ClassAndModuleChildren:
EnforcedStyle: compact
2 changes: 1 addition & 1 deletion lib/dotcrypt/CLI/app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class Dotcrypt::Cli::App < Thor
include Dotcrypt::Cli::Commands::Load
include Dotcrypt::Cli::Commands::Show
end
15 changes: 0 additions & 15 deletions lib/dotcrypt/CLI/commands/load.rb

This file was deleted.

53 changes: 53 additions & 0 deletions lib/dotcrypt/CLI/commands/show.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

module Dotcrypt::Cli::Commands::Show
class Handler
SERIALIZERS = {
json: Dotcrypt::Serializers::Json,
env: Dotcrypt::Serializers::Env
}.freeze

def initialize(file, **options)
@file = file
@options = options
end

def call = print(serializer.call(config))

private

def config
@config ||= Dotcrypt::Dhall.load_from(@file).then do |c|
next Dotcrypt::Flattener.call(c, separator: @options[:separator]) if @options[:flatten]

c
end
end

def serializer = SERIALIZERS[@options[:format].to_sym]
end

def self.included(thor) # rubocop:disable Metrics/MethodLength
thor.class_eval do
desc "show DHALL", "shows dhall file"

option :format, aliases: :f,
type: :string,
enum: ["json", "env"],
default: "json",
desc: "Output format."

option :flatten, aliases: :l,
type: :boolean,
default: false,
desc: "Whether to flatten the output. Always enabled for `env` format"

option :separator, aliases: :s,
type: :string,
default: "_",
desc: "Separator for flattening"

def show(file = "example.dhall") = Handler.new(file, **options).call
end
end
end
4 changes: 4 additions & 0 deletions lib/dotcrypt/dhall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ class << self
def load(dhall)
Dhall.load(dhall).sync.as_hash
end

def load_from(file)
load(File.read(file))
end
end
end
12 changes: 6 additions & 6 deletions lib/dotcrypt/dhall/as_hash.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
# frozen_string_literal: true

module Dotcrypt::Dhall::AsHash
refine Dhall::Record do
refine ::Dhall::Record do
def as_hash(*)
to_h.transform_values(&:as_hash)
end
end

refine Dhall::List do
refine ::Dhall::List do
def as_hash(*)
to_a.map(&:as_hash)
end
end

refine Dhall::Text do
refine ::Dhall::Text do
def as_hash(*)
to_s
end
end

refine Dhall::Natural do
refine ::Dhall::Natural do
def as_hash(*)
to_i
end
end

refine Dhall::Double do
refine ::Dhall::Double do
def as_hash(*)
to_f
end
end

refine Dhall::Builtins::None do
refine ::Dhall::Builtins::None do
def as_hash(*)
nil
end
Expand Down
7 changes: 7 additions & 0 deletions lib/dotcrypt/serializers/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require "shellwords"

class Dotcrypt::Serializers::Json < Dotcrypt::Serializers::Serializer
def call = JSON.pretty_generate(@config)
end

0 comments on commit b810b69

Please sign in to comment.