Class: Udb::Parameter
- Inherits:
-
TopLevelDatabaseObject
- Object
- DatabaseObject
- TopLevelDatabaseObject
- Udb::Parameter
- Extended by:
- T::Sig
- Includes:
- Idl::RuntimeParam
- Defined in:
- lib/udb/obj/parameter.rb
Overview
A parameter (AKA option, AKA implementation-defined value) supported by an extension
Defined Under Namespace
Classes: ConditionalSchema, NoMatchingSchemaError
Instance Attribute Summary collapse
-
#cfg_arch ⇒ ConfiguredArchitecture
readonly
The defining architecture.
- #schemas ⇒ Array<ConditionalSchema> readonly
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
sorts by name.
- #all_schemas ⇒ Array<Schema>
- #default ⇒ Object?
-
#defined_in_cfg?(cfg_arch) ⇒ SatisfiedResult
If this parameter is defined in
cfg_arch. - #idl_type ⇒ Idl::Type
- #initialize(yaml, data_path, cfg_arch) constructor
-
#maximal_idl_type ⇒ Idl::Type
returns the largest (compatibale with all) type of any possible schema.
-
#maximal_schema ⇒ Schema
returns the largest (compatibale with all) type of any possible schema.
-
#name ⇒ String
Parameter name.
-
#name_potentially_with_link(in_scope_exts) ⇒ String
Text to create a link to the parameter definition with the link text the parameter name.
-
#possible_schemas ⇒ Array<Schema>
List of schemas that are possible for this config.
- #requirements_condition ⇒ AbstractCondition
-
#schema ⇒ Schema
JSON Schema for this param.
-
#schema_known? ⇒ Boolean
whether or not the schema is unambiguously known since schemas can change based on parameter values and/or extension presence, non-full configs may not be able to know which schema applies.
-
#schema_type ⇒ String
Pretty convert extension schema to a string.
- #to_idl ⇒ String
- #value ⇒ Idl::RuntimeParam::ValueType
- #value_known? ⇒ Boolean
Constructor Details
#initialize(yaml, data_path, cfg_arch)
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/udb/obj/parameter.rb', line 74 def initialize(yaml, data_path, cfg_arch) super(yaml, data_path, cfg_arch) @schemas = T.let([], T::Array[ConditionalSchema]) if data.fetch("schema").key?("oneOf") data.fetch("schema").fetch("oneOf").each do |cond_schema| @schemas << ConditionalSchema.new(schema: Schema.new(cond_schema.fetch("schema")), cond: Condition.new(cond_schema.fetch("when"), @cfg_arch)) end else @schemas << ConditionalSchema.new(schema: Schema.new(data["schema"]), cond: AlwaysTrueCondition.new(cfg_arch)) end end |
Instance Attribute Details
#cfg_arch ⇒ ConfiguredArchitecture (readonly)
Returns The defining architecture.
25 26 27 |
# File 'lib/udb/obj/parameter.rb', line 25 def cfg_arch @cfg_arch end |
#schemas ⇒ Array<ConditionalSchema> (readonly)
113 114 115 |
# File 'lib/udb/obj/parameter.rb', line 113 def schemas @schemas end |
Instance Method Details
#<=>(other) ⇒ Integer?
sorts by name
213 214 215 216 217 |
# File 'lib/udb/obj/parameter.rb', line 213 def <=>(other) return nil unless other.is_a?(Idl::RuntimeParam) @name <=> other.name end |
#all_schemas ⇒ Array<Schema>
131 132 133 |
# File 'lib/udb/obj/parameter.rb', line 131 def all_schemas @schemas.map(&:schema) end |
#default ⇒ Object?
56 57 58 59 60 |
# File 'lib/udb/obj/parameter.rb', line 56 def default if T.cast(@data["schema"], T::Hash[String, Object]).key?("default") T.cast(@data["schema"], T::Hash[String, Object])["default"] end end |
#defined_in_cfg?(cfg_arch) ⇒ SatisfiedResult
Returns if this parameter is defined in cfg_arch.
193 194 195 |
# File 'lib/udb/obj/parameter.rb', line 193 def defined_in_cfg?(cfg_arch) defined_by_condition.satisfied_by_cfg_arch?(cfg_arch) end |
#idl_type ⇒ Idl::Type
138 139 140 141 142 143 144 |
# File 'lib/udb/obj/parameter.rb', line 138 def idl_type unless schema_known? raise "Schema is not known for parameter #{name} because more than one is possible given what we know about the configuration" end @idl_type ||= schema.to_idl_type.make_const.freeze end |
#maximal_idl_type ⇒ Idl::Type
returns the largest (compatibale with all) type of any possible schema
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/udb/obj/parameter.rb', line 148 def maximal_idl_type @maximal_idl_type ||= if schema_known? idl_type else idl_types = possible_schemas.map(&:to_idl_type) unless idl_types.all? { |t| t.kind == :bit } raise "TODO: paramter has multiple schemas that are not Bits" end max_width = idl_types.map(&:width).max do |a, b| if [a, b].include?(:unknown) a == :unknown ? 1 : -1 else (T.cast(a, Integer) <=> T.cast(b, Integer)) end end Idl::Type.new(:bits, width: max_width, qualifiers: [:const]) end end |
#maximal_schema ⇒ Schema
returns the largest (compatibale with all) type of any possible schema
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/udb/obj/parameter.rb', line 170 def maximal_schema @maximal_schema ||= if schema_known? schema else idl_types = possible_schemas.map(&:to_idl_type) unless idl_types.all? { |t| t.kind == :bits } raise "TODO: paramter has multiple schemas that are not Bits (#{idl_types.map(&:kind)})" end possible_schemas.max do |a, b| at = a.to_idl_type bt = b.to_idl_type if [at.width, bt.width].include?(:unknown) at.width == :unknown ? 1 : -1 else (T.cast(at.width, Integer) <=> T.cast(bt.width, Integer)) end end end end |
#name ⇒ String
Returns Parameter name.
29 |
# File 'lib/udb/obj/parameter.rb', line 29 def name = @name |
#name_potentially_with_link(in_scope_exts) ⇒ String
Returns Text to create a link to the parameter definition with the link text the parameter name. if only one extension defines the parameter, otherwise just the parameter name.
201 202 203 204 205 206 207 208 209 |
# File 'lib/udb/obj/parameter.rb', line 201 def name_potentially_with_link(in_scope_exts) helper = Class.new do include Udb::Helpers::TemplateHelpers end if in_scope_exts.size == 1 helper.new.link_to_udb_doc_ext_param(in_scope_exts.fetch(0).name, name, name) else name end end |
#possible_schemas ⇒ Array<Schema>
Returns list of schemas that are possible for this config.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/udb/obj/parameter.rb', line 119 def possible_schemas @possible_schemas ||= begin list = @schemas.select { |s| s.cond.could_be_satisfied_by_cfg_arch?(@cfg_arch) }.map(&:schema) if list.empty? raise NoMatchingSchemaError, "Parameter #{name} has no matching schema for #{@cfg_arch.name}" end list end end |
#requirements_condition ⇒ AbstractCondition
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/udb/obj/parameter.rb', line 38 def requirements_condition @requirements_condition ||= begin if @data["requirements"].nil? AlwaysTrueCondition.new(@cfg_arch) else Condition.new( @data.fetch("requirements"), @cfg_arch, input_file: Pathname.new(__source), input_line: source_line(["requirements"]) ) end end end |
#schema ⇒ Schema
Returns JSON Schema for this param.
104 105 106 107 108 109 110 |
# File 'lib/udb/obj/parameter.rb', line 104 def schema unless schema_known? raise "Schema is not known for parameter #{name} because more than one is possible given what we know about the configuration" end @schema ||= T.must(@schemas.find { |cond_schema| cond_schema.cond.satisfied_by_cfg_arch?(@cfg_arch) == SatisfiedResult::Yes }).schema end |
#schema_known? ⇒ Boolean
whether or not the schema is unambiguously known since schemas can change based on parameter values and/or extension presence, non-full configs may not be able to know which schema applies
91 92 93 94 95 96 97 98 99 |
# File 'lib/udb/obj/parameter.rb', line 91 def schema_known? @schema_known ||= begin if @schemas.size == 1 true else 1 == @schemas.count { |cond_schema| cond_schema.cond.could_be_satisfied_by_cfg_arch?(@cfg_arch) } end end end |
#schema_type ⇒ String
Pretty convert extension schema to a string.
33 34 35 |
# File 'lib/udb/obj/parameter.rb', line 33 def schema_type schema.to_pretty_s end |
#to_idl ⇒ String
220 |
# File 'lib/udb/obj/parameter.rb', line 220 def to_idl = "#{idl_type.to_idl} #{name}" |
#value ⇒ Idl::RuntimeParam::ValueType
226 |
# File 'lib/udb/obj/parameter.rb', line 226 def value = raise "Parameter value not known for #{name}" |
#value_known? ⇒ Boolean
223 |
# File 'lib/udb/obj/parameter.rb', line 223 def value_known? = false |