Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify protobuf compiler default field values #1543

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions lib/tapioca/dsl/compilers/protobuf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,27 +250,21 @@ def field_of(descriptor)
value_type = type_of(value)
type = "Google::Protobuf::Map[#{key_type}, #{value_type}]"

default_args = [key.type.inspect, value.type.inspect]
default_args << value_type if [:enum, :message].include?(value.type)

Field.new(
name: descriptor.name,
type: type,
init_type: "T.nilable(T.any(#{type}, T::Hash[#{key_type}, #{value_type}]))",
default: "Google::Protobuf::Map.new(#{default_args.join(", ")})",
default: "T.unsafe(nil)",
)
else
elem_type = type_of(descriptor)
type = "Google::Protobuf::RepeatedField[#{elem_type}]"

default_args = [descriptor.type.inspect]
default_args << elem_type if [:enum, :message].include?(descriptor.type)

Field.new(
name: descriptor.name,
type: type,
init_type: "T.nilable(T.any(#{type}, T::Array[#{elem_type}]))",
default: "Google::Protobuf::RepeatedField.new(#{default_args.join(", ")})",
default: "T.unsafe(nil)",
)
end
else
Expand Down
10 changes: 5 additions & 5 deletions spec/tapioca/dsl/compilers/protobuf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def value_type=(value); end

class Cart
sig { params(customer_ids: T.nilable(T.any(Google::Protobuf::RepeatedField[Integer], T::Array[Integer])), indices: T.nilable(T.any(Google::Protobuf::RepeatedField[Google::Protobuf::UInt64Value], T::Array[Google::Protobuf::UInt64Value]))).void }
def initialize(customer_ids: Google::Protobuf::RepeatedField.new(:int32), indices: Google::Protobuf::RepeatedField.new(:message, Google::Protobuf::UInt64Value)); end
def initialize(customer_ids: T.unsafe(nil), indices: T.unsafe(nil)); end

sig { void }
def clear_customer_ids; end
Expand Down Expand Up @@ -330,7 +330,7 @@ def indices=(value); end

class Cart
sig { params(customers: T.nilable(T.any(Google::Protobuf::Map[String, Integer], T::Hash[String, Integer])), stores: T.nilable(T.any(Google::Protobuf::Map[String, Google::Protobuf::UInt64Value], T::Hash[String, Google::Protobuf::UInt64Value]))).void }
def initialize(customers: Google::Protobuf::Map.new(:string, :int32), stores: Google::Protobuf::Map.new(:string, :message, Google::Protobuf::UInt64Value)); end
def initialize(customers: T.unsafe(nil), stores: T.unsafe(nil)); end

sig { void }
def clear_customers; end
Expand Down Expand Up @@ -529,7 +529,7 @@ class Cart; end

class Google::Protobuf::Struct
sig { params(fields: T.nilable(T.any(Google::Protobuf::Map[String, Google::Protobuf::Value], T::Hash[String, Google::Protobuf::Value]))).void }
def initialize(fields: Google::Protobuf::Map.new(:string, :message, Google::Protobuf::Value)); end
def initialize(fields: T.unsafe(nil)); end

sig { void }
def clear_fields; end
Expand Down Expand Up @@ -573,7 +573,7 @@ def fields=(value); end

class Cart
sig { params(items: T.nilable(T.any(Google::Protobuf::RepeatedField[T.untyped], T::Array[T.untyped])), tables: T.nilable(T.any(Google::Protobuf::Map[String, T.untyped], T::Hash[String, T.untyped]))).void }
def initialize(items: Google::Protobuf::RepeatedField.new(:message, T.untyped), tables: Google::Protobuf::Map.new(:string, :message, T.untyped)); end
def initialize(items: T.unsafe(nil), tables: T.unsafe(nil)); end

sig { void }
def clear_items; end
Expand Down Expand Up @@ -642,7 +642,7 @@ def tables=(value); end

class Cart
sig { params(progress: T.nilable(T.any(Google::Protobuf::Map[String, Cart::Progress], T::Hash[String, Cart::Progress]))).void }
def initialize(progress: Google::Protobuf::Map.new(:string, :message, Cart::Progress)); end
def initialize(progress: T.unsafe(nil)); end

sig { void }
def clear_progress; end
Expand Down