|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Mindee |
| 4 | + module Input |
| 5 | + # Data Schema Field. |
| 6 | + class DataSchemaField |
| 7 | + # @return [String] Display name for the field, also impacts inference results. |
| 8 | + attr_reader :title |
| 9 | + # @return [String] Name of the field in the data schema. |
| 10 | + attr_reader :name |
| 11 | + # @return [Boolean] Whether this field can contain multiple values. |
| 12 | + attr_reader :is_array |
| 13 | + # @return [String] Data type of the field. |
| 14 | + attr_reader :type |
| 15 | + # @return [Array<String>, nil] Allowed values when type is `classification`. Leave empty for other types. |
| 16 | + attr_reader :classification_values |
| 17 | + # @return [Boolean, nil] Whether to remove duplicate values in the array. |
| 18 | + # Only applicable if `is_array` is True. |
| 19 | + attr_reader :unique_values |
| 20 | + # @return [String, nil] Detailed description of what this field represents. |
| 21 | + attr_reader :description |
| 22 | + # @return [String, nil] Optional extraction guidelines. |
| 23 | + attr_reader :guidelines |
| 24 | + # @return [Array<Hash>, nil] Nested fields. |
| 25 | + attr_reader :nested_fields |
| 26 | + |
| 27 | + # @param field [Hash] |
| 28 | + def initialize(field) |
| 29 | + field.transform_keys!(&:to_sym) |
| 30 | + @name = field[:name] |
| 31 | + @title = field[:title] |
| 32 | + @is_array = field[:is_array] |
| 33 | + @type = field[:type] |
| 34 | + @classification_values = field[:classification_values] |
| 35 | + @unique_values = field[:unique_values] |
| 36 | + @description = field[:description] |
| 37 | + @guidelines = field[:guidelines] |
| 38 | + @nested_fields = field[:nested_fields] |
| 39 | + end |
| 40 | + |
| 41 | + # @return [Hash] |
| 42 | + def to_hash |
| 43 | + out = { |
| 44 | + name: @name, |
| 45 | + title: @title, |
| 46 | + is_array: @is_array, |
| 47 | + type: @type, |
| 48 | + } # @type var out: Hash[Symbol, untyped] |
| 49 | + out[:classification_values] = @classification_values unless @classification_values.nil? |
| 50 | + out[:unique_values] = @unique_values unless @unique_values.nil? |
| 51 | + out[:description] = @description unless @description.nil? |
| 52 | + out[:guidelines] = @guidelines unless @guidelines.nil? |
| 53 | + out[:nested_fields] = @nested_fields unless @nested_fields.nil? |
| 54 | + out |
| 55 | + end |
| 56 | + |
| 57 | + # @return [String] |
| 58 | + def to_s |
| 59 | + to_hash.to_json |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + # The structure to completely replace the data schema of the model. |
| 64 | + class DataSchemaReplace |
| 65 | + # @return [Array<DataSchemaField>] Subfields when type is `nested_object`. Leave empty for other types. |
| 66 | + attr_reader :fields |
| 67 | + |
| 68 | + # @param data_schema_replace [Hash] |
| 69 | + def initialize(data_schema_replace) |
| 70 | + data_schema_replace.transform_keys!(&:to_sym) |
| 71 | + fields_list = data_schema_replace[:fields] |
| 72 | + raise Mindee::Errors::MindeeError, 'Invalid Data Schema provided.' if fields_list.nil? |
| 73 | + raise TypeError, 'Data Schema replacement fields cannot be empty.' if fields_list.empty? |
| 74 | + |
| 75 | + @fields = fields_list.map { |field| DataSchemaField.new(field) } |
| 76 | + end |
| 77 | + |
| 78 | + # @return [Hash] |
| 79 | + def to_hash |
| 80 | + { fields: @fields.map(&:to_hash) } |
| 81 | + end |
| 82 | + |
| 83 | + # @return [String] |
| 84 | + def to_s |
| 85 | + to_hash.to_json |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + # Modify the Data Schema. |
| 90 | + class DataSchema |
| 91 | + # @return [Mindee::Input::DataSchemaReplace] |
| 92 | + attr_reader :replace |
| 93 | + |
| 94 | + # @param data_schema [Hash, String] |
| 95 | + def initialize(data_schema) |
| 96 | + case data_schema |
| 97 | + when String |
| 98 | + parsed = JSON.parse(data_schema.to_s, object_class: Hash) |
| 99 | + parsed.transform_keys!(&:to_sym) |
| 100 | + @replace = DataSchemaReplace.new(parsed[:replace]) |
| 101 | + when Hash |
| 102 | + data_schema.transform_keys!(&:to_sym) |
| 103 | + @replace = if data_schema[:replace].is_a?(DataSchemaReplace) |
| 104 | + data_schema[:replace] |
| 105 | + else |
| 106 | + DataSchemaReplace.new(data_schema[:replace]) |
| 107 | + end |
| 108 | + when DataSchema |
| 109 | + @replace = data_schema.replace |
| 110 | + else |
| 111 | + raise TypeError, 'Invalid Data Schema provided.' |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + # @return [Hash] |
| 116 | + def to_hash |
| 117 | + { replace: @replace.to_hash } |
| 118 | + end |
| 119 | + |
| 120 | + # @return [String] |
| 121 | + def to_s |
| 122 | + to_hash.to_json |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments