Class: Gapic::Schema::Resource
- Inherits:
-
Object
- Object
- Gapic::Schema::Resource
- Extended by:
- Forwardable
- Defined in:
- lib/gapic/schema/wrappers.rb
Overview
Wrapper for a protobuf Resource.
Unlike most wrappers, this does not subclass the Proto wrapper because
it does not use the fields exposed by that wrapper (address
, docs
,
etc.) This is here principally to augment the resource definition with
information about resource parent-child relationships.
Resource parentage is defined implicitly by path patterns. The algorithm is as follows:
- If the final segment of a pattern is an ID segment (i.e.
*
or some{name}
) then remove it and the previous segment (which we assume to be the corresponding collection identifier, as described in AIP-122.) The resulting pattern is what we expect a parent to have. - If the final segment is static, then assume the pattern represents a singleton resource (AIP-156) and remove only that one segment. The resulting pattern is what we expect a parent to have.
The #parsed_parent_patterns method returns the set of patterns we expect of parents. It is then possible to search for resources with those patterns to determine what the parents are.
Instance Attribute Summary collapse
-
#descriptor ⇒ Array<Gapic::Schema::ResourceDescriptor>
readonly
The resource descriptor.
-
#parent ⇒ Gapic::Schema::File, Gapic::Schema::Message
The parent object.
-
#parent_resources ⇒ Array<Gapic::Schema::Resource>
readonly
Parent resources.
-
#parsed_parent_patterns ⇒ Object
readonly
Returns the value of attribute parsed_parent_patterns.
-
#parsed_patterns ⇒ Array<String>
readonly
The template form of the patterns.
Instance Method Summary collapse
-
#containing_api ⇒ Gapic::Schema::Api
Returns the "root" of this schema.
-
#containing_file ⇒ Gapic::Schema::File
Returns the file containing this proto entity.
-
#initialize(descriptor) ⇒ Resource
constructor
Initializes a resource object.
-
#name_field ⇒ String
The field on the resource that designates the resource name field.
-
#pattern ⇒ Array<String>
The set of patterns.
-
#type ⇒ String
The resource type string.
Constructor Details
#initialize(descriptor) ⇒ Resource
Initializes a resource object.
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
# File 'lib/gapic/schema/wrappers.rb', line 1103 def initialize descriptor @parent = nil @descriptor = descriptor patterns = descriptor.pattern.map do |pattern| Gapic::PathPattern.parse pattern end.freeze @parsed_patterns = patterns.map(&:template).compact.uniq.freeze @parsed_parent_patterns = patterns.map(&:parent_template).compact.uniq.freeze @parent_resources = [] end |
Instance Attribute Details
#descriptor ⇒ Array<Gapic::Schema::ResourceDescriptor> (readonly)
Returns The resource descriptor.
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/gapic/schema/wrappers.rb', line 1090 class Resource extend Forwardable attr_reader :descriptor attr_reader :parsed_patterns attr_reader :parsed_parent_patterns attr_reader :parent_resources attr_accessor :parent # Initializes a resource object. # @param descriptor [Google::Api::ResourceDescriptor] the protobuf # representation of this resource. def initialize descriptor @parent = nil @descriptor = descriptor patterns = descriptor.pattern.map do |pattern| Gapic::PathPattern.parse pattern end.freeze @parsed_patterns = patterns.map(&:template).compact.uniq.freeze @parsed_parent_patterns = patterns.map(&:parent_template).compact.uniq.freeze @parent_resources = [] end # Returns the "root" of this schema. # @return [Gapic::Schema::Api] def containing_api parent&.containing_api end # Returns the file containing this proto entity # @return [Gapic::Schema::File] def containing_file parent&.containing_file end # @!method type # @return [String] the resource type string. # @!method pattern # @return [Array<String>] the set of patterns. # @!method name_field # @return [String] the field on the resource that designates the # resource name field. If omitted, this is assumed to be "name". def_delegators( :descriptor, :type, :pattern, :name_field ) end |
#parent ⇒ Gapic::Schema::File, Gapic::Schema::Message
Returns The parent object.
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/gapic/schema/wrappers.rb', line 1090 class Resource extend Forwardable attr_reader :descriptor attr_reader :parsed_patterns attr_reader :parsed_parent_patterns attr_reader :parent_resources attr_accessor :parent # Initializes a resource object. # @param descriptor [Google::Api::ResourceDescriptor] the protobuf # representation of this resource. def initialize descriptor @parent = nil @descriptor = descriptor patterns = descriptor.pattern.map do |pattern| Gapic::PathPattern.parse pattern end.freeze @parsed_patterns = patterns.map(&:template).compact.uniq.freeze @parsed_parent_patterns = patterns.map(&:parent_template).compact.uniq.freeze @parent_resources = [] end # Returns the "root" of this schema. # @return [Gapic::Schema::Api] def containing_api parent&.containing_api end # Returns the file containing this proto entity # @return [Gapic::Schema::File] def containing_file parent&.containing_file end # @!method type # @return [String] the resource type string. # @!method pattern # @return [Array<String>] the set of patterns. # @!method name_field # @return [String] the field on the resource that designates the # resource name field. If omitted, this is assumed to be "name". def_delegators( :descriptor, :type, :pattern, :name_field ) end |
#parent_resources ⇒ Array<Gapic::Schema::Resource> (readonly)
Returns Parent resources.
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/gapic/schema/wrappers.rb', line 1090 class Resource extend Forwardable attr_reader :descriptor attr_reader :parsed_patterns attr_reader :parsed_parent_patterns attr_reader :parent_resources attr_accessor :parent # Initializes a resource object. # @param descriptor [Google::Api::ResourceDescriptor] the protobuf # representation of this resource. def initialize descriptor @parent = nil @descriptor = descriptor patterns = descriptor.pattern.map do |pattern| Gapic::PathPattern.parse pattern end.freeze @parsed_patterns = patterns.map(&:template).compact.uniq.freeze @parsed_parent_patterns = patterns.map(&:parent_template).compact.uniq.freeze @parent_resources = [] end # Returns the "root" of this schema. # @return [Gapic::Schema::Api] def containing_api parent&.containing_api end # Returns the file containing this proto entity # @return [Gapic::Schema::File] def containing_file parent&.containing_file end # @!method type # @return [String] the resource type string. # @!method pattern # @return [Array<String>] the set of patterns. # @!method name_field # @return [String] the field on the resource that designates the # resource name field. If omitted, this is assumed to be "name". def_delegators( :descriptor, :type, :pattern, :name_field ) end |
#parsed_parent_patterns ⇒ Object (readonly)
Returns the value of attribute parsed_parent_patterns.
1095 1096 1097 |
# File 'lib/gapic/schema/wrappers.rb', line 1095 def parsed_parent_patterns @parsed_parent_patterns end |
#parsed_patterns ⇒ Array<String> (readonly)
Returns The template form of the
patterns. Template means all ID segments are replaced by asterisks
to remove non-structural differences due to different names being
used.
For example, if a pattern is "projects/{project}""
, the
corresponding parsed pattern would be "projects/*"]
.
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/gapic/schema/wrappers.rb', line 1090 class Resource extend Forwardable attr_reader :descriptor attr_reader :parsed_patterns attr_reader :parsed_parent_patterns attr_reader :parent_resources attr_accessor :parent # Initializes a resource object. # @param descriptor [Google::Api::ResourceDescriptor] the protobuf # representation of this resource. def initialize descriptor @parent = nil @descriptor = descriptor patterns = descriptor.pattern.map do |pattern| Gapic::PathPattern.parse pattern end.freeze @parsed_patterns = patterns.map(&:template).compact.uniq.freeze @parsed_parent_patterns = patterns.map(&:parent_template).compact.uniq.freeze @parent_resources = [] end # Returns the "root" of this schema. # @return [Gapic::Schema::Api] def containing_api parent&.containing_api end # Returns the file containing this proto entity # @return [Gapic::Schema::File] def containing_file parent&.containing_file end # @!method type # @return [String] the resource type string. # @!method pattern # @return [Array<String>] the set of patterns. # @!method name_field # @return [String] the field on the resource that designates the # resource name field. If omitted, this is assumed to be "name". def_delegators( :descriptor, :type, :pattern, :name_field ) end |
Instance Method Details
#containing_api ⇒ Gapic::Schema::Api
Returns the "root" of this schema.
1116 1117 1118 |
# File 'lib/gapic/schema/wrappers.rb', line 1116 def containing_api parent&.containing_api end |
#containing_file ⇒ Gapic::Schema::File
Returns the file containing this proto entity
1122 1123 1124 |
# File 'lib/gapic/schema/wrappers.rb', line 1122 def containing_file parent&.containing_file end |
#name_field ⇒ String
Returns the field on the resource that designates the resource name field. If omitted, this is assumed to be "name".
1133 1134 1135 1136 1137 1138 |
# File 'lib/gapic/schema/wrappers.rb', line 1133 def_delegators( :descriptor, :type, :pattern, :name_field ) |
#pattern ⇒ Array<String>
Returns the set of patterns.
1133 1134 1135 1136 1137 1138 |
# File 'lib/gapic/schema/wrappers.rb', line 1133 def_delegators( :descriptor, :type, :pattern, :name_field ) |
#type ⇒ String
Returns the resource type string.
1133 1134 1135 1136 1137 1138 |
# File 'lib/gapic/schema/wrappers.rb', line 1133 def_delegators( :descriptor, :type, :pattern, :name_field ) |