Class: Udb::Parameter

Inherits:
TopLevelDatabaseObject show all
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

Instance Method Summary collapse

Constructor Details

#initialize(yaml, data_path, cfg_arch)

Parameters:



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_archConfiguredArchitecture (readonly)

Returns The defining architecture.

Returns:



25
26
27
# File 'lib/udb/obj/parameter.rb', line 25

def cfg_arch
  @cfg_arch
end

#schemasArray<ConditionalSchema> (readonly)

Returns:



113
114
115
# File 'lib/udb/obj/parameter.rb', line 113

def schemas
  @schemas
end

Instance Method Details

#<=>(other) ⇒ Integer?

sorts by name

Parameters:

  • other (T.untyped)

Returns:

  • (Integer, nil)


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_schemasArray<Schema>

Returns:



131
132
133
# File 'lib/udb/obj/parameter.rb', line 131

def all_schemas
  @schemas.map(&:schema)
end

#defaultObject?

Returns:

  • (Object, nil)


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.

Parameters:

Returns:

  • (SatisfiedResult)

    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_typeIdl::Type

Returns:

  • (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_typeIdl::Type

returns the largest (compatibale with all) type of any possible schema

Returns:

  • (Idl::Type)


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_schemaSchema

returns the largest (compatibale with all) type of any possible schema

Returns:



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

#nameString

Returns Parameter name.

Returns:

  • (String)

    Parameter name



29
# File 'lib/udb/obj/parameter.rb', line 29

def name = @name

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.

Parameters:

  • exts

    List of all in-scope extensions that define this parameter.

  • in_scope_exts (Array<Extension>)

Returns:

  • (String)

    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_schemasArray<Schema>

Returns list of schemas that are possible for this config.

Returns:

  • (Array<Schema>)

    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_conditionAbstractCondition

Returns:



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

#schemaSchema

Returns JSON Schema for this param.

Returns:

  • (Schema)

    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

Returns:

  • (Boolean)


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_typeString

Pretty convert extension schema to a string.

Returns:

  • (String)


33
34
35
# File 'lib/udb/obj/parameter.rb', line 33

def schema_type
  schema.to_pretty_s
end

#to_idlString

Returns:

  • (String)


220
# File 'lib/udb/obj/parameter.rb', line 220

def to_idl = "#{idl_type.to_idl} #{name}"

#valueIdl::RuntimeParam::ValueType

Returns:

  • (Idl::RuntimeParam::ValueType)


226
# File 'lib/udb/obj/parameter.rb', line 226

def value = raise "Parameter value not known for #{name}"

#value_known?Boolean

Returns:

  • (Boolean)


223
# File 'lib/udb/obj/parameter.rb', line 223

def value_known? = false