|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'fhir_models' |
| 4 | +require 'rubygems/package' |
| 5 | +require 'zlib' |
| 6 | + |
| 7 | +require_relative '../repositories/igs' |
| 8 | + |
| 9 | +module Inferno |
| 10 | + module Entities |
| 11 | + # IG is a wrapper class around the relevant concepts inside an IG. |
| 12 | + # Not everything within an IG is currently used by Inferno. |
| 13 | + class IG < Entity |
| 14 | + ATTRIBUTES = [ |
| 15 | + :id, |
| 16 | + :profiles, |
| 17 | + :extensions, |
| 18 | + :value_sets, |
| 19 | + :search_params, |
| 20 | + :examples |
| 21 | + ].freeze |
| 22 | + |
| 23 | + include Inferno::Entities::Attributes |
| 24 | + |
| 25 | + def initialize(params) |
| 26 | + super(params, ATTRIBUTES) |
| 27 | + |
| 28 | + @profiles = [] |
| 29 | + @extensions = [] |
| 30 | + @value_sets = [] |
| 31 | + @examples = [] |
| 32 | + @search_params = [] |
| 33 | + end |
| 34 | + |
| 35 | + def self.from_file(ig_path) |
| 36 | + raise "#{ig_path} does not exist" unless File.exist?(ig_path) |
| 37 | + |
| 38 | + # fhir_models by default logs the entire content of non-FHIR files |
| 39 | + # which could be things like a package.json |
| 40 | + original_logger = FHIR.logger |
| 41 | + FHIR.logger = Logger.new('/dev/null') |
| 42 | + |
| 43 | + if File.directory?(ig_path) |
| 44 | + from_directory(ig_path) |
| 45 | + elsif ig_path.end_with? '.tgz' |
| 46 | + from_tgz(ig_path) |
| 47 | + else |
| 48 | + raise "Unable to load #{ig_path} as it does not appear to be a directory or a .tgz file" |
| 49 | + end |
| 50 | + ensure |
| 51 | + FHIR.logger = original_logger if defined? original_logger |
| 52 | + end |
| 53 | + |
| 54 | + def self.from_tgz(ig_path) |
| 55 | + tar = Gem::Package::TarReader.new( |
| 56 | + Zlib::GzipReader.open(ig_path) |
| 57 | + ) |
| 58 | + |
| 59 | + ig = IG.new({}) |
| 60 | + |
| 61 | + tar.each do |entry| |
| 62 | + next if skip_item?(entry.full_name, entry.directory?) |
| 63 | + |
| 64 | + begin |
| 65 | + resource = FHIR::Json.from_json(entry.read) |
| 66 | + next if resource.nil? |
| 67 | + |
| 68 | + ig.handle_resource(resource, entry.full_name) |
| 69 | + rescue StandardError |
| 70 | + next |
| 71 | + end |
| 72 | + end |
| 73 | + ig |
| 74 | + end |
| 75 | + |
| 76 | + def self.from_directory(ig_path) |
| 77 | + ig = IG.new({}) |
| 78 | + |
| 79 | + Dir.glob("#{ig_path}/**/*") do |f| |
| 80 | + next if skip_item?(f, File.directory?(f)) |
| 81 | + |
| 82 | + begin |
| 83 | + resource = FHIR::Json.from_json(File.read(f)) |
| 84 | + next if resource.nil? |
| 85 | + |
| 86 | + ig.handle_resource(resource, f) |
| 87 | + rescue StandardError |
| 88 | + next |
| 89 | + end |
| 90 | + end |
| 91 | + ig |
| 92 | + end |
| 93 | + |
| 94 | + # These files aren't FHIR resources |
| 95 | + FILES_TO_SKIP = ['package.json', 'validation-summary.json'].freeze |
| 96 | + |
| 97 | + def self.skip_item?(relative_path, is_directory) |
| 98 | + return true if is_directory |
| 99 | + |
| 100 | + file_name = relative_path.split('/').last |
| 101 | + |
| 102 | + # TODO: consider making these regexes we can iterate over in a single loop |
| 103 | + return true unless file_name.end_with? '.json' |
| 104 | + return true unless relative_path.start_with? 'package/' |
| 105 | + |
| 106 | + return true if file_name.start_with? '.' # ignore hidden files |
| 107 | + return true if file_name.end_with? '.openapi.json' |
| 108 | + return true if FILES_TO_SKIP.include? file_name |
| 109 | + |
| 110 | + false |
| 111 | + end |
| 112 | + |
| 113 | + def handle_resource(resource, relative_path) |
| 114 | + case resource.resourceType |
| 115 | + when 'StructureDefinition' |
| 116 | + if resource.type == 'Extension' |
| 117 | + extensions.push resource |
| 118 | + else |
| 119 | + profiles.push resource |
| 120 | + end |
| 121 | + when 'ValueSet' |
| 122 | + value_sets.push resource |
| 123 | + when 'SearchParameter' |
| 124 | + search_params.push resource |
| 125 | + when 'ImplementationGuide' |
| 126 | + @id = extract_package_id(resource) |
| 127 | + else |
| 128 | + examples.push(resource) if relative_path.start_with? 'package/example' |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + def extract_package_id(ig_resource) |
| 133 | + "#{ig_resource.id}##{ig_resource.version || 'current'}" |
| 134 | + end |
| 135 | + |
| 136 | + # @private |
| 137 | + def add_self_to_repository |
| 138 | + repository.insert(self) |
| 139 | + end |
| 140 | + |
| 141 | + # @private |
| 142 | + def repository |
| 143 | + Inferno::Repositories::IGs.new |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | +end |
0 commit comments