Sha256: 213267b70252632fd3f6f73c8ba5a57528c5f0778b2745fd8294e463491d84d6

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Kril
  # Extracts schemas from avro generated java files.
  module SchemaExtractor
    # Parse schemas from avro generated java files and
    # load them into the schema repository.
    #
    # source_dir - root directory of java files [String]
    # output_dir - schema repository [String]
    # returns    - [nil]
    def self.extract(source_dir:, output_dir:)
      find_java_files(source_dir) do |file|
        schema = parse_avro_java_class(file)
        SchemaHandler.new(schemas_path: output_dir).process(schema)
      end
      nil
    end

    module_function

    def find_java_files(root_dir)
      old_dir = Dir.pwd
      Dir.chdir(root_dir)
      java_files = File.join('**', '*.java')
      Dir.glob(java_files) do |file|
        yield File.new(file)
      end
    ensure
      Dir.chdir(old_dir)
    end

    def dejavafy(java_string)
      java_string.split('","').join.gsub(/\\?\\"/, '"')
    end

    def parse_avro_java_class(file)
      result = file.each_line do |line|
        extraction = line[/SCHEMA.*parse\("(.*)"\);/, 1]
        break dejavafy(extraction) if extraction
      end
      result.is_a?(File) ? nil : result
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kril-0.2.4 lib/kril/schema_extractor.rb
kril-0.2.3 lib/kril/schema_extractor.rb
kril-0.2.2 lib/kril/schema_extractor.rb