Class: Udb::Parameter

Inherits:
Object
  • Object
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ext, name, data)

Parameters:

  • ext (Extension)
  • name (String)
  • data (Hash{String => T.untyped})


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/udb/obj/parameter.rb', line 69

def initialize(ext, name, data)

  @cfg_arch = T.let(ext.cfg_arch, ConfiguredArchitecture)
  @data = T.let(data, T::Hash[String, T.untyped])
  @name = T.let(name, String)
  @desc = T.let(T.cast(data["description"], String), String)
  @schema = T.let(Schema.new(data["schema"]), Schema)
  @extra_validation = T.let(data.key?("extra_validation") ? T.let(T.cast(data["extra_validation"], String), String) : nil, T.nilable(String))
  also_defined_in_array = []
  also_defined_in_data = data["also_defined_in"]
  unless also_defined_in_data.nil?
    other_ext = T.let(nil, T.nilable(Extension))
    if also_defined_in_data.is_a?(String)
      other_ext_name = also_defined_in_data
      other_ext = @cfg_arch.extension(other_ext_name)
      raise "Definition error in #{ext.name}.#{name}: #{other_ext_name} is not a known extension" if other_ext.nil?

      also_defined_in_array << other_ext
    else
      unless also_defined_in_data.is_a?(Array) && also_defined_in_data.all? { |e| e.is_a?(String) }
        raise "schema error: also_defined_in should be a string or array of strings"
      end

      also_defined_in_data.each do |other_ext_name|
        other_ext = @cfg_arch.extension(other_ext_name)
        raise "Definition error in #{ext.name}.#{name}: #{also_defined_in_data} is not a known extension" if other_ext.nil?

        also_defined_in_array << other_ext
      end
    end
  end
  @exts = T.let([ext] + also_defined_in_array, T::Array[Extension])
  @idl_type = T.let(@schema.to_idl_type.make_const.freeze, ::Idl::Type)
  @when = T.let(nil, T.nilable(ExtensionRequirementExpression))
end

Instance Attribute Details

#cfg_archConfiguredArchitecture (readonly)

Returns The defining architecture.

Returns:



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

def cfg_arch
  @cfg_arch
end

#descString (readonly)

Returns Asciidoc description.

Returns:

  • (String)

    Asciidoc description



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

def desc
  @desc
end

#extra_validationnil, String (readonly)

Returns:

  • (nil)

    If there is no extra validation

  • (String, nil)

    Ruby code to perform validation above and beyond JSON schema



41
42
43
# File 'lib/udb/obj/parameter.rb', line 41

def extra_validation
  @extra_validation
end

#extsArray<Extension> (readonly)

Some parameters are defined by multiple extensions (e.g., CACHE_BLOCK_SIZE by Zicbom and Zicboz). When defined in multiple places, the parameter must mean the exact same thing.

Returns:

  • (Array<Extension>)

    The extension(s) that define this parameter



48
49
50
# File 'lib/udb/obj/parameter.rb', line 48

def exts
  @exts
end

#idl_typeIdl::Type (readonly)

Returns:

  • (Idl::Type)


52
53
54
# File 'lib/udb/obj/parameter.rb', line 52

def idl_type
  @idl_type
end

#schemaSchema (readonly)

Returns JSON Schema for this param.

Returns:

  • (Schema)

    JSON Schema for this param



36
37
38
# File 'lib/udb/obj/parameter.rb', line 36

def schema
  @schema
end

Instance Method Details

#<=>(other) ⇒ Integer?

sorts by name

Parameters:

Returns:

  • (Integer, nil)


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

def <=>(other) = @name <=> other.name

#defaultObject?

Returns:

  • (Object, nil)


62
63
64
65
66
# File 'lib/udb/obj/parameter.rb', line 62

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:



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/udb/obj/parameter.rb', line 133

def defined_in_cfg?(cfg_arch)
  if @exts.none? { |ext| cfg_arch.possible_extensions.any? { |e| e.name == ext.name } }
    return SatisfiedResult::No
  end

  if @when.nil?
    return SatisfiedResult::Yes
  end

  @when.satisfied_by_cfg_arch?(cfg_arch)
end

#nameString

Returns Parameter name.

Returns:

  • (String)

    Parameter name



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

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 (Array<Extension>)

    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.



149
150
151
152
153
154
155
156
157
# File 'lib/udb/obj/parameter.rb', line 149

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

#schema_typeString

Pretty convert extension schema to a string.

Returns:

  • (String)


56
57
58
# File 'lib/udb/obj/parameter.rb', line 56

def schema_type
  @schema.to_pretty_s
end

#to_idlString

Returns:

  • (String)


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

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

#valueIdl::RuntimeParam::ValueType

Returns:

  • (Idl::RuntimeParam::ValueType)


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

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

#value_known?Boolean

Returns:

  • (Boolean)


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

def value_known? = false

#whenExtensionRequirementExpression

Returns Condition when the parameter exists.

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/udb/obj/parameter.rb', line 107

def when
  @when ||=
    if @data["when"].nil?
      # the parent extension is implictly required
      cond =
        if @exts.size > 1
          { "anyOf" => @exts.map { |ext| { "name" => ext.name, "version" => ">= #{ext.min_version.version_str}" } }}
        else
          { "name" => @exts.fetch(0).name, "version" => ">= #{@exts.fetch(0).min_version.version_str}"}
        end
      ExtensionRequirementExpression.new(cond, @cfg_arch)
    else
      # the parent extension is implictly required
      cond =
        if @exts.size > 1
          { "allOf" => [{"anyOf" => @exts.map { |ext| { "name" => ext.name, "version" => ">= #{ext.min_version.version_str}" } } }, @data["when"]] }
        else
          { "allOf" => [ { "name" => @exts.fetch(0).name, "version" => ">= #{@exts.fetch(0).min_version.version_str}"}, @data["when"]] }
        end
      ExtensionRequirementExpression.new(cond, @cfg_arch)
    end
end