Class: Gapic::Schema::Resource

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

Instance Method Summary collapse

Constructor Details

#initialize(descriptor) ⇒ Resource

Initializes a resource object.

Parameters:



955
956
957
958
959
960
961
962
963
964
# File 'lib/gapic/schema/wrappers.rb', line 955

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

#descriptorArray<Gapic::Schema::ResourceDescriptor> (readonly)

Returns The resource descriptor.

Returns:

  • (Array<Gapic::Schema::ResourceDescriptor>)

    The resource descriptor.



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/gapic/schema/wrappers.rb', line 942

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

#parentGapic::Schema::File, Gapic::Schema::Message

Returns The parent object.

Returns:



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/gapic/schema/wrappers.rb', line 942

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_resourcesArray<Gapic::Schema::Resource> (readonly)

Returns Parent resources.

Returns:



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/gapic/schema/wrappers.rb', line 942

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

Returns the value of attribute parsed_parent_patterns.



947
948
949
# File 'lib/gapic/schema/wrappers.rb', line 947

def parsed_parent_patterns
  @parsed_parent_patterns
end

#parsed_patternsArray<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/*"].

Returns:

  • (Array<String>)

    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/*"].



942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/gapic/schema/wrappers.rb', line 942

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_apiGapic::Schema::Api

Returns the "root" of this schema.

Returns:



968
969
970
# File 'lib/gapic/schema/wrappers.rb', line 968

def containing_api
  parent&.containing_api
end

#containing_fileGapic::Schema::File

Returns the file containing this proto entity

Returns:



974
975
976
# File 'lib/gapic/schema/wrappers.rb', line 974

def containing_file
  parent&.containing_file
end

#name_fieldString

Returns the field on the resource that designates the resource name field. If omitted, this is assumed to be "name".

Returns:

  • (String)

    the field on the resource that designates the resource name field. If omitted, this is assumed to be "name".



985
986
987
988
989
990
# File 'lib/gapic/schema/wrappers.rb', line 985

def_delegators(
  :descriptor,
  :type,
  :pattern,
  :name_field
)

#patternArray<String>

Returns the set of patterns.

Returns:

  • (Array<String>)

    the set of patterns.



985
986
987
988
989
990
# File 'lib/gapic/schema/wrappers.rb', line 985

def_delegators(
  :descriptor,
  :type,
  :pattern,
  :name_field
)

#typeString

Returns the resource type string.

Returns:

  • (String)

    the resource type string.



985
986
987
988
989
990
# File 'lib/gapic/schema/wrappers.rb', line 985

def_delegators(
  :descriptor,
  :type,
  :pattern,
  :name_field
)