The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
require "json"
require "yaml"
module Pyrite::Kubernetes
abstract class Spec
include ::JSON::Serializable
include ::YAML::Serializable
macro def_parser
macro finalized
{% if @type.abstract? %}
{% puts @type.subclasses %}
def self.new(pull : JSON::PullParser)
location = pull.location
{% begin %}
string = pull.read_raw
{% for subtype in @type.subclasses %}
begin
return {{type}}.from_json(string)
rescue JSON::ParseException
# Ignore
end
{% end %}
raise JSON::ParseException.new("Couldn't parse #{self} from #{string}", *location)
{% end %}
end
{% end %}
end
end
macro inherited
def_parser
end
def_parser
end
end
\{% puts @type.subclasses %}
❯ psykube status --cluster dev
NAME STATUS RESTARTS AGE
api-665c77fbb9-2dlk9 Running 0 23h2m
api-665c77fbb9-nwmx8 Running 0 23h1m
api-665c77fbb9-rjp6k Running 0 23h1m
def foo(&)
yield -1
end
foo do |v|
puts v.abs
end
JSON::Serializable
works and would like something similar. Any suggestions?
Showing last frame. Use --error-trace for full trace.
In configs_wather.cr:60:27
60 | if change.includes?(monitor_path)
^--------
Error: no overload matches 'String#includes?' with type (Hash(String, String) | String)
Overloads are:
configs = {"bind_conf" => {"path" => "/usr/home/system/configs/bind", "ext" => "conf", "cmd" => {"exec" => "rndc", "args" => "reconfig"}}, }
if File.file?("#{watch_log_path}")
File.each_line("#{watch_log_path}") do |line|
split_line = line.split(" ")
file_path = split_line[0] + split_line[2]
file_extension = split_line[2].split(".").last if split_line[2].split(".").size > 1
events[file_path] = {"file_name" => split_line[2], "event" => split_line[1], "ext" => file_extension}
end
#cleanup the file after read
File.open "#{watch_log_path}", "w+" { }
end
#dynamic event processing begin below.
#copy the event and clean up the current.
process_events = events.clone
events.clear
configs.each do |job,param|
puts "Processing #{job}"
process_events.each do |change, event_params|
monitor_path = param["path"]
if change.includes?(monitor_path)
puts "Processing change #{change}"
end
end
end
@type.instance_vars
in a module to get the variables of a class that it was included into?
module Test
annotation Flag
end
module Configuration
macro included
{% puts @type %}
{% puts @type.annotations(Flag) %}
{% puts @type.instance_vars %}
end
end
struct Config
include Configuration
@[Flag(name: "a", short: "a", type: String?)]
@a : String?
end
end
module Configuration
def initialize
{% puts @type %}
{% puts @type.annotations(Flag) %}
{% puts @type.instance_vars %}
end
end