Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Commit

Permalink
Create Fields using unsorted positional types
Browse files Browse the repository at this point in the history
This change allows for positional types, types with an accompanying position value,
such as those defined by a Hash without names, to be unsorted.
The initializer now sorts those positional types.
  • Loading branch information
blowmage committed Jun 11, 2018
1 parent 99ad6c6 commit 5678964
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
20 changes: 14 additions & 6 deletions google-cloud-spanner/lib/google/cloud/spanner/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,22 @@ def initialize types
raise ArgumentError, "can only accept Array or Hash"
end

# Verify positional arguments are in order
types.each_with_index do |type, index|
if type.is_a?(Array) && type.first.is_a?(Integer)
raise "types must be in order" if type.first != index
end
sorted_types, unsorted_types = types.partition do |type|
type.is_a?(Array) && type.count == 2 && type.first.is_a?(Integer)
end

if sorted_types.count != sorted_types.map(&:first).uniq.count
raise ArgumentError, "cannot specify position more than once"
end

@grpc_fields = types.map { |type| to_grpc_field type }
@grpc_fields = Array.new(types.count) do |index|
sorted_type = sorted_types.assoc index
if sorted_type
to_grpc_field sorted_type.last
else
to_grpc_field unsorted_types.shift
end
end
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@
end

it "creates with an unsorted mixed array of fields" do
skip "Not yet implemented"
fields = Google::Cloud::Spanner::Fields.new [[:name, :STRING], :BOOL, [0, :INT64]]

fields.types.must_equal [:INT64, :STRING, :BOOL]
fields.keys.must_equal [0, :name, 2]
fields.pairs.must_equal [[0, :INT64], [:name, :STRING], [2, :BOOL]]
fields.to_a.must_equal [:INT64, [:name, :STRING], :BOOL]
fields.to_a.must_equal [:INT64, :STRING, :BOOL]
fields.to_h.must_equal({ 0=>:INT64, name: :STRING, 2=>:BOOL })
end

Expand Down Expand Up @@ -87,13 +86,12 @@
end

it "creates with an unsorted mixed hash of fields" do
skip "Not yet implemented"
fields = Google::Cloud::Spanner::Fields.new name: :STRING, 2=>:BOOL, 0=>:INT64

fields.types.must_equal [:INT64, :STRING, :BOOL]
fields.keys.must_equal [0, :name, 2]
fields.pairs.must_equal [[0, :INT64], [:name, :STRING], [2, :BOOL]]
fields.to_a.must_equal [:INT64, [:name, :STRING], :BOOL]
fields.to_a.must_equal [:INT64, :STRING, :BOOL]
fields.to_h.must_equal({ 0=>:INT64, name: :STRING, 2=>:BOOL })
end
end

0 comments on commit 5678964

Please sign in to comment.