# frozen_string_literal: true require 'json' module FlippRubyKafka module Utils # Generates the Microservice Platform schema validation file. # Run this before committing schema changes (or include it in your CircleCI # config if you don't mind having to build the same image multiple times). # Run this using `rails runner` or in Rails console if you're using Rails. class PlatformSchemaValidation # @param base_path [String] Set this to Rails.root if running Rails. def initialize(base_path: '') @base_path ||= FlippRubyKafka.config.schema.path.gsub(%r{^#{base_path}\/}, '') end # @param klass [Class < FlippRubyKafka::Producer] # @return [Hash] def process_producer(klass) schema = klass.config[:schema] topic = klass.config[:topic] return if topic.blank? namespace = klass.config[:namespace] || FlippRubyKafka.config.producers.schema_namespace path = namespace.tr('.', '/') { 'file' => File.join(@base_path, path, "#{schema}.avsc"), 'subject' => "#{topic}-value" } end # @param hash [Hash] # @return [Hash] def process_consumer(hash) klass = hash['handler'].constantize topic = hash['topic'] schema = klass.config[:schema] return if schema.blank? namespace = klass.config[:namespace] || '' path = namespace.tr('.', '/') { 'file' => File.join(@base_path, path, "#{schema}.avsc"), 'subject' => "#{topic}-value" } end # @return [String] def generate_config @full_config = [] FlippRubyKafka::Producer.descendants.each do |klass| next if klass.topic.blank? config = process_producer(klass) @full_config << config if config end Phobos.config['listeners'].each do |h| config = process_consumer(h) @full_config << config if config end { environments: { default: @full_config } } end # :nodoc: def run full_json = generate_config File.write('schema-validation.json', JSON.pretty_generate(full_json)) end end end end