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:

  • descriptor (Google::Protobuf::ResourceDescriptor) —

    the protobuf representation of this resource.



780
781
782
783
784
785
786
787
788
789
# File 'lib/gapic/schema/wrappers.rb', line 780

def initialize descriptor
  @parent = nil
  @descriptor = descriptor
  @parsed_patterns = descriptor.pattern.map do |pattern|
    pattern.split("/").map do |segment|
      segment =~ %r{\{[^/\}]+(=[^\}]+)?\}} ? "*" : segment
    end.freeze
  end.freeze
  @parent_resources = []
end

Instance Attribute Details

#descriptor ⇒ Array<Gapic::Schema::ResourceDescriptor> (readonly)

Returns The resource descriptor.

Returns:

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

    The resource descriptor.



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/gapic/schema/wrappers.rb', line 772

class Resource
  extend Forwardable
  attr_reader :descriptor, :parsed_patterns, :parent_resources
  attr_accessor :parent

  # Initializes a resource object.
  # @param descriptor [Google::Protobuf::ResourceDescriptor] the protobuf
  #   representation of this resource.
  def initialize descriptor
    @parent = nil
    @descriptor = descriptor
    @parsed_patterns = descriptor.pattern.map do |pattern|
      pattern.split("/").map do |segment|
        segment =~ %r{\{[^/\}]+(=[^\}]+)?\}} ? "*" : segment
      end.freeze
    end.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

  # Returns parsed patterns for the expected parents.
  # @return [Array<Array<String>>]
  def parsed_parent_patterns
    @parsed_patterns.map do |pat|
      parent = pat.last =~ /^\*\*?$/ ? pat[0...-2] : pat[0...-1]
      parent.empty? ? nil : parent
    end.compact.uniq
  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.

Returns:



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/gapic/schema/wrappers.rb', line 772

class Resource
  extend Forwardable
  attr_reader :descriptor, :parsed_patterns, :parent_resources
  attr_accessor :parent

  # Initializes a resource object.
  # @param descriptor [Google::Protobuf::ResourceDescriptor] the protobuf
  #   representation of this resource.
  def initialize descriptor
    @parent = nil
    @descriptor = descriptor
    @parsed_patterns = descriptor.pattern.map do |pattern|
      pattern.split("/").map do |segment|
        segment =~ %r{\{[^/\}]+(=[^\}]+)?\}} ? "*" : segment
      end.freeze
    end.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

  # Returns parsed patterns for the expected parents.
  # @return [Array<Array<String>>]
  def parsed_parent_patterns
    @parsed_patterns.map do |pat|
      parent = pat.last =~ /^\*\*?$/ ? pat[0...-2] : pat[0...-1]
      parent.empty? ? nil : parent
    end.compact.uniq
  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.

Returns:



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/gapic/schema/wrappers.rb', line 772

class Resource
  extend Forwardable
  attr_reader :descriptor, :parsed_patterns, :parent_resources
  attr_accessor :parent

  # Initializes a resource object.
  # @param descriptor [Google::Protobuf::ResourceDescriptor] the protobuf
  #   representation of this resource.
  def initialize descriptor
    @parent = nil
    @descriptor = descriptor
    @parsed_patterns = descriptor.pattern.map do |pattern|
      pattern.split("/").map do |segment|
        segment =~ %r{\{[^/\}]+(=[^\}]+)?\}} ? "*" : segment
      end.freeze
    end.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

  # Returns parsed patterns for the expected parents.
  # @return [Array<Array<String>>]
  def parsed_parent_patterns
    @parsed_patterns.map do |pat|
      parent = pat.last =~ /^\*\*?$/ ? pat[0...-2] : pat[0...-1]
      parent.empty? ? nil : parent
    end.compact.uniq
  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_patterns ⇒ Array<Array<String>> (readonly)

Returns The normalized, segmented forms of the patterns. Normalized means all ID segments are replaced by asterisks to remove non-structural differences due to different names being used. Segmented means simply split on slashes. For example, if a pattern is "projects/{project}"", the corresponding parsed pattern would be ["projects", "*"].

Returns:

  • (Array<Array<String>>) —

    The normalized, segmented forms of the patterns. Normalized means all ID segments are replaced by asterisks to remove non-structural differences due to different names being used. Segmented means simply split on slashes. For example, if a pattern is "projects/{project}"", the corresponding parsed pattern would be ["projects", "*"].



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/gapic/schema/wrappers.rb', line 772

class Resource
  extend Forwardable
  attr_reader :descriptor, :parsed_patterns, :parent_resources
  attr_accessor :parent

  # Initializes a resource object.
  # @param descriptor [Google::Protobuf::ResourceDescriptor] the protobuf
  #   representation of this resource.
  def initialize descriptor
    @parent = nil
    @descriptor = descriptor
    @parsed_patterns = descriptor.pattern.map do |pattern|
      pattern.split("/").map do |segment|
        segment =~ %r{\{[^/\}]+(=[^\}]+)?\}} ? "*" : segment
      end.freeze
    end.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

  # Returns parsed patterns for the expected parents.
  # @return [Array<Array<String>>]
  def parsed_parent_patterns
    @parsed_patterns.map do |pat|
      parent = pat.last =~ /^\*\*?$/ ? pat[0...-2] : pat[0...-1]
      parent.empty? ? nil : parent
    end.compact.uniq
  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.

Returns:



793
794
795
# File 'lib/gapic/schema/wrappers.rb', line 793

def containing_api
  parent&.containing_api
end

#containing_file ⇒ Gapic::Schema::File

Returns the file containing this proto entity

Returns:



799
800
801
# File 'lib/gapic/schema/wrappers.rb', line 799

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".

Returns:

  • (String) —

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



819
820
821
822
823
824
# File 'lib/gapic/schema/wrappers.rb', line 819

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

#parsed_parent_patterns ⇒ Array<Array<String>>

Returns parsed patterns for the expected parents.

Returns:

  • (Array<Array<String>>)


805
806
807
808
809
810
# File 'lib/gapic/schema/wrappers.rb', line 805

def parsed_parent_patterns
  @parsed_patterns.map do |pat|
    parent = pat.last =~ /^\*\*?$/ ? pat[0...-2] : pat[0...-1]
    parent.empty? ? nil : parent
  end.compact.uniq
end

#pattern ⇒ Array<String>

Returns the set of patterns.

Returns:

  • (Array<String>) —

    the set of patterns.



819
820
821
822
823
824
# File 'lib/gapic/schema/wrappers.rb', line 819

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

#type ⇒ String

Returns the resource type string.

Returns:

  • (String) —

    the resource type string.



819
820
821
822
823
824
# File 'lib/gapic/schema/wrappers.rb', line 819

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