diff --git a/codegen/projections/rails_json/lib/rails_json/types.rb b/codegen/projections/rails_json/lib/rails_json/types.rb index e2e0f443e..c3f783dc7 100644 --- a/codegen/projections/rails_json/lib/rails_json/types.rb +++ b/codegen/projections/rails_json/lib/rails_json/types.rb @@ -1925,50 +1925,30 @@ class StringValue < MyUnion def to_h { string_value: super(__getobj__) } end - - def to_s - "#" - end end class BooleanValue < MyUnion def to_h { boolean_value: super(__getobj__) } end - - def to_s - "#" - end end class NumberValue < MyUnion def to_h { number_value: super(__getobj__) } end - - def to_s - "#" - end end class BlobValue < MyUnion def to_h { blob_value: super(__getobj__) } end - - def to_s - "#" - end end class TimestampValue < MyUnion def to_h { timestamp_value: super(__getobj__) } end - - def to_s - "#" - end end # Enum, one of: ["Foo", "Baz", "Bar", "1", "0"] @@ -1976,50 +1956,30 @@ class EnumValue < MyUnion def to_h { enum_value: super(__getobj__) } end - - def to_s - "#" - end end class ListValue < MyUnion def to_h { list_value: super(__getobj__) } end - - def to_s - "#" - end end class MapValue < MyUnion def to_h { map_value: super(__getobj__) } end - - def to_s - "#" - end end class StructureValue < MyUnion def to_h { structure_value: super(__getobj__) } end - - def to_s - "#" - end end class RenamedStructureValue < MyUnion def to_h { renamed_structure_value: super(__getobj__) } end - - def to_s - "#" - end end class Unknown < MyUnion @@ -2519,10 +2479,6 @@ class Quit < PlayerAction def to_h { quit: super(__getobj__) } end - - def to_s - "#" - end end class Unknown < PlayerAction @@ -3424,10 +3380,6 @@ class Greeting < UnionPayload def to_h { greeting: super(__getobj__) } end - - def to_s - "#" - end end class Unknown < UnionPayload @@ -3450,30 +3402,18 @@ class Foo < UnionWithJsonName def to_h { foo: super(__getobj__) } end - - def to_s - "#" - end end class Bar < UnionWithJsonName def to_h { bar: super(__getobj__) } end - - def to_s - "#" - end end class Baz < UnionWithJsonName def to_h { baz: super(__getobj__) } end - - def to_s - "#" - end end class Unknown < UnionWithJsonName diff --git a/codegen/projections/white_label/lib/white_label/types.rb b/codegen/projections/white_label/lib/white_label/types.rb index 32a0c3967..bea8a9baa 100644 --- a/codegen/projections/white_label/lib/white_label/types.rb +++ b/codegen/projections/white_label/lib/white_label/types.rb @@ -175,26 +175,26 @@ class Defaults attr_accessor(*MEMBERS) def to_s - "#" end @@ -348,26 +348,26 @@ class DefaultsTestOutput attr_accessor(*MEMBERS) def to_s - "#" end @@ -631,17 +631,17 @@ class KitchenSinkInput attr_accessor(*MEMBERS) def to_s - "#" + '#' end end @@ -746,17 +746,17 @@ class KitchenSinkOutput attr_accessor(*MEMBERS) def to_s - "#" + '#' end end @@ -1170,10 +1170,6 @@ class String < Union def to_h { string: super(__getobj__) } end - - def to_s - "#" - end end # This docstring should be different than KitchenSink struct member. diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/types/StructureGenerator.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/types/StructureGenerator.java index eca3a953b..218c6ac80 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/types/StructureGenerator.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/types/StructureGenerator.java @@ -191,25 +191,29 @@ private void renderToSMethod(RubyCodeWriter writer) { writer .openBlock("\ndef to_s") - .write("\"#<$L \"\\", fullQualifiedShapeName) + .write("'#<$L ' \\", fullQualifiedShapeName) .indent(); while (iterator.hasNext()) { MemberShape memberShape = iterator.next(); String key = symbolProvider.toMemberName(memberShape); String value = "#{" + key + " || 'nil'}"; + boolean memberLiteral = false; if (memberShape.isBlobShape() || memberShape.isStringShape()) { // Strings are wrapped in quotes value = "\"" + value + "\""; + memberLiteral = true; } else if (memberShape.getMemberTrait(model, SensitiveTrait.class).isPresent()) { - value = "\\\"[SENSITIVE]\\\""; + value = "[SENSITIVE]"; + memberLiteral = true; } + String quote = memberLiteral ? "'" : "\""; if (iterator.hasNext()) { - writer.write("\"$L=$L, \"\\", key, value); + writer.write("$3L$1L=$2L, $3L \\", key, value, quote); } else { - writer.write("\"$L=$L>\"", key, value); + writer.write("$3L$1L=$2L>$3L", key, value, quote); } } writer diff --git a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/types/UnionGenerator.java b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/types/UnionGenerator.java index bda9accfa..df0bb69a8 100644 --- a/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/types/UnionGenerator.java +++ b/codegen/smithy-ruby-codegen/src/main/java/software/amazon/smithy/ruby/codegen/generators/types/UnionGenerator.java @@ -112,19 +112,17 @@ public void render() { } private void renderUnionToSMethod(RubyCodeWriter writer, MemberShape memberShape) { - String fullQualifiedShapeName = settings.getModule() + "::Types::" - + symbolProvider.toMemberName(memberShape); - - writer.write("") - .openBlock("def to_s"); - - if (memberShape.getMemberTrait(model, SensitiveTrait.class).isPresent()) { - writer.write("\"#<$L [SENSITIVE]>\"", fullQualifiedShapeName); - } else { - writer.write("\"#<$L #{__getobj__ || 'nil'}>\"", fullQualifiedShapeName); + if (!memberShape.getMemberTrait(model, SensitiveTrait.class).isPresent()) { + return; } - writer.closeBlock("end"); + String fullQualifiedShapeName = settings.getModule() + "::Types::" + + symbolProvider.toMemberName(memberShape); + writer + .write("") + .openBlock("def to_s") + .write("\"#<$L [SENSITIVE]>\"", fullQualifiedShapeName) + .closeBlock("end"); } private class UnionToHValueRbsVisitor extends ShapeVisitor.Default { diff --git a/hearth/lib/hearth/structure.rb b/hearth/lib/hearth/structure.rb index 14702413a..22b685e7d 100644 --- a/hearth/lib/hearth/structure.rb +++ b/hearth/lib/hearth/structure.rb @@ -40,6 +40,23 @@ def to_h(obj = self) end alias to_hash to_h + # Returns a string representation of the Structure. + def to_s(obj = self) + case obj + when Union + "#<#{obj.class.name} #{obj.__getobj__ || 'nil'}>" + when Structure + _to_s_structure(obj) + when Hash + _to_s_hash(obj) + when Array + _to_s_array(obj) + else + obj.to_s + end + end + alias to_string to_s + private def _to_h_structure(obj) @@ -58,5 +75,23 @@ def _to_h_hash(obj) def _to_h_array(obj) obj.collect { |value| to_hash(value) } end + + def _to_s_structure(obj) + members = obj.class::MEMBERS.map do |member| + value = to_string(obj.send(member)) + " #{member}=#{value.empty? ? 'nil' : value}" + end + "#<#{self.class.name}#{members.join(',')}>" + end + + def _to_s_hash(obj) + obj.transform_values do |value| + to_string(value) + end + end + + def _to_s_array(obj) + obj.collect { |value| to_string(value) } + end end end diff --git a/hearth/spec/hearth/structure_spec.rb b/hearth/spec/hearth/structure_spec.rb index 518cb1a7e..e1a70e944 100644 --- a/hearth/spec/hearth/structure_spec.rb +++ b/hearth/spec/hearth/structure_spec.rb @@ -46,7 +46,7 @@ def _defaults end end - describe '#to_hash' do + describe '#to_h' do it 'serializes nested structs to a hash' do expected = { struct_value: { value: 'foo', default_value: 'default' }, @@ -65,5 +65,20 @@ def _defaults expect(subject.to_h).to eq expected end end + + describe '#to_s' do + it 'returns a string representation of the structure' do + actual = subject.to_s + expect(actual).to include(subject.class.name) + expect(actual).to include(subject.struct_value.to_s) + expect(actual).to include(subject.array_value.first.to_s) + expect(actual).to include(subject.array_value.last.to_s) + expect(actual).to include(subject.hash_value[:key].to_s) + expect(actual).to include(subject.value) + expect(actual).to include(subject.union_value.to_s) + expect(actual).to include(subject.some_object.to_s) + expect(actual).to include(subject.default_value) + end + end end end diff --git a/hearth/spec/hearth/union_spec.rb b/hearth/spec/hearth/union_spec.rb index 862ff534f..5146fa66c 100644 --- a/hearth/spec/hearth/union_spec.rb +++ b/hearth/spec/hearth/union_spec.rb @@ -22,5 +22,12 @@ def to_h expect(subject.to_h).to eq(string_value: 'union') end end + + describe '#to_s' do + it 'returns a string representation' do + expect(subject.to_s) + .to eq('#') + end + end end end