aws-sdk-code-generator 0.1.0.pre → 0.2.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +5 -5
  2. data/lib/aws-sdk-code-generator.rb +68 -75
  3. data/lib/aws-sdk-code-generator/api.rb +130 -0
  4. data/lib/aws-sdk-code-generator/apply_docs.rb +15 -2
  5. data/lib/aws-sdk-code-generator/client_constructor.rb +39 -0
  6. data/lib/aws-sdk-code-generator/client_operation_documentation.rb +268 -0
  7. data/lib/aws-sdk-code-generator/client_operation_list.rb +148 -0
  8. data/lib/aws-sdk-code-generator/client_response_structure_example.rb +108 -0
  9. data/lib/aws-sdk-code-generator/code_builder.rb +146 -133
  10. data/lib/aws-sdk-code-generator/crosslink.rb +42 -0
  11. data/lib/aws-sdk-code-generator/docstring.rb +199 -0
  12. data/lib/aws-sdk-code-generator/error_list.rb +77 -0
  13. data/lib/aws-sdk-code-generator/errors.rb +2 -0
  14. data/lib/aws-sdk-code-generator/eventstream_example.rb +220 -0
  15. data/lib/aws-sdk-code-generator/gem_builder.rb +19 -25
  16. data/lib/aws-sdk-code-generator/hash_formatter.rb +5 -2
  17. data/lib/aws-sdk-code-generator/helper.rb +77 -61
  18. data/lib/aws-sdk-code-generator/plugin_list.rb +146 -0
  19. data/lib/aws-sdk-code-generator/resource_action.rb +69 -0
  20. data/lib/aws-sdk-code-generator/resource_action_code.rb +57 -0
  21. data/lib/aws-sdk-code-generator/resource_association.rb +37 -0
  22. data/lib/aws-sdk-code-generator/resource_attribute.rb +76 -0
  23. data/lib/aws-sdk-code-generator/resource_batch_action.rb +56 -0
  24. data/lib/aws-sdk-code-generator/resource_batch_action_code.rb +136 -0
  25. data/lib/aws-sdk-code-generator/resource_batch_action_documentation.rb +108 -0
  26. data/lib/aws-sdk-code-generator/resource_batch_builder.rb +212 -0
  27. data/lib/aws-sdk-code-generator/resource_builder.rb +48 -0
  28. data/lib/aws-sdk-code-generator/resource_client_request.rb +62 -0
  29. data/lib/aws-sdk-code-generator/resource_client_request_documentation.rb +81 -0
  30. data/lib/aws-sdk-code-generator/resource_client_request_params.rb +86 -0
  31. data/lib/aws-sdk-code-generator/resource_data_method.rb +60 -0
  32. data/lib/aws-sdk-code-generator/resource_has_association.rb +117 -0
  33. data/lib/aws-sdk-code-generator/resource_has_many_association.rb +52 -0
  34. data/lib/aws-sdk-code-generator/resource_has_many_association_code.rb +76 -0
  35. data/lib/aws-sdk-code-generator/resource_identifier.rb +44 -0
  36. data/lib/aws-sdk-code-generator/resource_identifiers_method.rb +29 -0
  37. data/lib/aws-sdk-code-generator/resource_load_method.rb +68 -0
  38. data/lib/aws-sdk-code-generator/resource_method.rb +22 -0
  39. data/lib/aws-sdk-code-generator/resource_skip_params.rb +36 -0
  40. data/lib/aws-sdk-code-generator/resource_value_source.rb +68 -0
  41. data/lib/aws-sdk-code-generator/resource_waiter.rb +80 -0
  42. data/lib/aws-sdk-code-generator/service.rb +30 -7
  43. data/lib/aws-sdk-code-generator/shared_example.rb +131 -0
  44. data/lib/aws-sdk-code-generator/syntax_example.rb +60 -0
  45. data/lib/aws-sdk-code-generator/syntax_example_hash.rb +174 -0
  46. data/lib/aws-sdk-code-generator/underscore.rb +10 -5
  47. data/lib/aws-sdk-code-generator/view.rb +33 -0
  48. data/lib/aws-sdk-code-generator/views.rb +2 -0
  49. data/lib/aws-sdk-code-generator/views/apig_endpoint_class.rb +25 -0
  50. data/lib/aws-sdk-code-generator/views/apig_readme.rb +32 -0
  51. data/lib/aws-sdk-code-generator/views/async_client_class.rb +68 -0
  52. data/lib/aws-sdk-code-generator/views/authorizer_class.rb +17 -0
  53. data/lib/aws-sdk-code-generator/views/client_api_module.rb +583 -0
  54. data/lib/aws-sdk-code-generator/views/client_class.rb +93 -0
  55. data/lib/aws-sdk-code-generator/views/docstring.rb +27 -0
  56. data/lib/aws-sdk-code-generator/views/errors_module.rb +32 -0
  57. data/lib/aws-sdk-code-generator/views/event_streams_module.rb +149 -0
  58. data/lib/aws-sdk-code-generator/views/features/env.rb +9 -0
  59. data/lib/aws-sdk-code-generator/views/features/smoke.rb +51 -0
  60. data/lib/aws-sdk-code-generator/views/features/smoke_step_definitions.rb +26 -0
  61. data/lib/aws-sdk-code-generator/views/features/step_definitions.rb +2 -0
  62. data/lib/aws-sdk-code-generator/views/gemspec.rb +39 -5
  63. data/lib/aws-sdk-code-generator/views/resource_class.rb +122 -0
  64. data/lib/aws-sdk-code-generator/views/root_resource_class.rb +58 -0
  65. data/lib/aws-sdk-code-generator/views/service_module.rb +30 -14
  66. data/lib/aws-sdk-code-generator/views/spec/spec_helper.rb +9 -0
  67. data/lib/aws-sdk-code-generator/views/types_module.rb +294 -0
  68. data/lib/aws-sdk-code-generator/views/version.rb +2 -0
  69. data/lib/aws-sdk-code-generator/views/waiters_module.rb +37 -0
  70. data/lib/aws-sdk-code-generator/waiter.rb +95 -0
  71. data/lib/aws-sdk-code-generator/yard_option_tag.rb +43 -0
  72. metadata +61 -68
  73. data/lib/aws-sdk-code-generator/dsl/access_control_statement.rb +0 -23
  74. data/lib/aws-sdk-code-generator/dsl/attribute_accessor.rb +0 -43
  75. data/lib/aws-sdk-code-generator/dsl/attribute_reader.rb +0 -11
  76. data/lib/aws-sdk-code-generator/dsl/attribute_writer.rb +0 -11
  77. data/lib/aws-sdk-code-generator/dsl/autoload_statement.rb +0 -15
  78. data/lib/aws-sdk-code-generator/dsl/block_param.rb +0 -11
  79. data/lib/aws-sdk-code-generator/dsl/class.rb +0 -27
  80. data/lib/aws-sdk-code-generator/dsl/code_literal.rb +0 -66
  81. data/lib/aws-sdk-code-generator/dsl/code_object.rb +0 -33
  82. data/lib/aws-sdk-code-generator/dsl/docstring.rb +0 -36
  83. data/lib/aws-sdk-code-generator/dsl/eigenclass.rb +0 -15
  84. data/lib/aws-sdk-code-generator/dsl/extend_statement.rb +0 -12
  85. data/lib/aws-sdk-code-generator/dsl/formatter.rb +0 -25
  86. data/lib/aws-sdk-code-generator/dsl/include_statement.rb +0 -17
  87. data/lib/aws-sdk-code-generator/dsl/main.rb +0 -105
  88. data/lib/aws-sdk-code-generator/dsl/method.rb +0 -108
  89. data/lib/aws-sdk-code-generator/dsl/module.rb +0 -167
  90. data/lib/aws-sdk-code-generator/dsl/option_tag.rb +0 -36
  91. data/lib/aws-sdk-code-generator/dsl/param.rb +0 -43
  92. data/lib/aws-sdk-code-generator/dsl/param_list.rb +0 -38
  93. data/lib/aws-sdk-code-generator/dsl/return_tag.rb +0 -19
  94. data/lib/aws-sdk-code-generator/dsl/tag_default.rb +0 -20
  95. data/lib/aws-sdk-code-generator/dsl/tag_docstring.rb +0 -27
  96. data/lib/aws-sdk-code-generator/dsl/tag_type.rb +0 -18
  97. data/lib/aws-sdk-code-generator/generators/client_api_module.rb +0 -334
  98. data/lib/aws-sdk-code-generator/generators/client_class.rb +0 -389
  99. data/lib/aws-sdk-code-generator/generators/client_operation_documentation.rb +0 -166
  100. data/lib/aws-sdk-code-generator/generators/errors_module.rb +0 -25
  101. data/lib/aws-sdk-code-generator/generators/resource/action.rb +0 -88
  102. data/lib/aws-sdk-code-generator/generators/resource/batch_builder.rb +0 -211
  103. data/lib/aws-sdk-code-generator/generators/resource/builder.rb +0 -50
  104. data/lib/aws-sdk-code-generator/generators/resource/client_getter.rb +0 -15
  105. data/lib/aws-sdk-code-generator/generators/resource/client_request.rb +0 -49
  106. data/lib/aws-sdk-code-generator/generators/resource/client_request_docs.rb +0 -97
  107. data/lib/aws-sdk-code-generator/generators/resource/client_request_params.rb +0 -88
  108. data/lib/aws-sdk-code-generator/generators/resource/collection_class.rb +0 -180
  109. data/lib/aws-sdk-code-generator/generators/resource/data_attribute_getter.rb +0 -24
  110. data/lib/aws-sdk-code-generator/generators/resource/data_loaded_method.rb +0 -18
  111. data/lib/aws-sdk-code-generator/generators/resource/data_method.rb +0 -49
  112. data/lib/aws-sdk-code-generator/generators/resource/exists_method.rb +0 -29
  113. data/lib/aws-sdk-code-generator/generators/resource/extract_identifier_method.rb +0 -32
  114. data/lib/aws-sdk-code-generator/generators/resource/has_association.rb +0 -101
  115. data/lib/aws-sdk-code-generator/generators/resource/has_many_association.rb +0 -108
  116. data/lib/aws-sdk-code-generator/generators/resource/identifier_getter.rb +0 -26
  117. data/lib/aws-sdk-code-generator/generators/resource/identifiers_method.rb +0 -28
  118. data/lib/aws-sdk-code-generator/generators/resource/initialize_method.rb +0 -67
  119. data/lib/aws-sdk-code-generator/generators/resource/load_method.rb +0 -65
  120. data/lib/aws-sdk-code-generator/generators/resource/value_source.rb +0 -68
  121. data/lib/aws-sdk-code-generator/generators/resource/waiter_method.rb +0 -61
  122. data/lib/aws-sdk-code-generator/generators/resource_class.rb +0 -325
  123. data/lib/aws-sdk-code-generator/generators/response_structure_example.rb +0 -83
  124. data/lib/aws-sdk-code-generator/generators/root_resource_class.rb +0 -42
  125. data/lib/aws-sdk-code-generator/generators/service_documentation.rb +0 -64
  126. data/lib/aws-sdk-code-generator/generators/shared_example.rb +0 -132
  127. data/lib/aws-sdk-code-generator/generators/structure_type_class.rb +0 -95
  128. data/lib/aws-sdk-code-generator/generators/syntax_example.rb +0 -169
  129. data/lib/aws-sdk-code-generator/generators/types_module.rb +0 -52
  130. data/lib/aws-sdk-code-generator/generators/waiter_class.rb +0 -62
  131. data/lib/aws-sdk-code-generator/generators/waiters_module.rb +0 -20
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwsSdkCodeGenerator
4
+ class ResourceMethod
5
+
6
+ # @return [String]
7
+ attr_accessor :method_name
8
+
9
+ # @return [String, nil]
10
+ attr_accessor :arguments
11
+
12
+ # @return [String]
13
+ attr_accessor :code
14
+
15
+ # @return [String, nil]
16
+ attr_accessor :documentation
17
+
18
+ # @return [String, nil]
19
+ attr_accessor :alias
20
+
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwsSdkCodeGenerator
4
+ class ResourceSkipParams
5
+ class << self
6
+
7
+ def compute(shape, request)
8
+ if shape
9
+ shape['members'].inject([]) do |skip, (member_name, member_ref)|
10
+ skip << member_name if request_param?(member_name, request)
11
+ skip
12
+ end
13
+ else
14
+ []
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def request_param?(member_name, request)
21
+ request.fetch('params', []).any? do |param|
22
+ if
23
+ param['target'].match(/^#{member_name}\b/) &&
24
+ !(param['target'].include?('[') &&
25
+ param['target'].include?('.'))
26
+ then
27
+ true
28
+ else
29
+ false
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwsSdkCodeGenerator
4
+ class ResourceValueSource < String
5
+
6
+ def initialize(value)
7
+ super(send("param_#{underscore(value['source'])}", value))
8
+ end
9
+
10
+ private
11
+
12
+ def param_identifier(param)
13
+ "@" + underscore(param['name'])
14
+ end
15
+
16
+ def param_data(param)
17
+ path = param['path']
18
+ if path == '@'
19
+ 'data'
20
+ elsif path.match(/^(\w(\[0\])?)+(\.\w+)*$/)
21
+ data_path = underscore_path(path).gsub(/\w+/) { |word| "[:#{word}]" }
22
+ # In case resource model path contains x.[:0].y
23
+ 'data' + data_path.gsub(/\[\[\:/, '[').gsub(/\]\]/, ']').gsub(/\./, '')
24
+ else
25
+ raise "unsupported path: #{path.inspect}"
26
+ end
27
+ end
28
+
29
+ def param_response(param)
30
+ if param['path'] == '@'
31
+ 'resp.data'
32
+ else
33
+ 'resp.data.' + underscore_path(param['path'].gsub(/\[\]/, "[0]"))
34
+ end
35
+ end
36
+
37
+ def param_request_parameter(param)
38
+ if param['path'].match(/^\w+(\.\w+)*$/)
39
+ "options" + path_parts(param['path']).map { |part| "[:#{part}]" }.join
40
+ else
41
+ raise "unsupported path expression: #{param['path']}"
42
+ end
43
+ end
44
+
45
+ def param_string(param)
46
+ param['value'].inspect
47
+ end
48
+ alias param_integer param_string
49
+ alias param_boolean param_string
50
+
51
+ def param_input(param)
52
+ underscore(param['target'])
53
+ end
54
+
55
+ def underscore(str)
56
+ Underscore.underscore(str)
57
+ end
58
+
59
+ def underscore_path(path)
60
+ path.gsub(/\w+/) { |part| underscore(part) }
61
+ end
62
+
63
+ def path_parts(path)
64
+ path.split('.').map { |part| underscore(part) }
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwsSdkCodeGenerator
4
+ class ResourceWaiter
5
+
6
+ # @return [String]
7
+ attr_accessor :name
8
+
9
+ attr_accessor :client_waiter_name
10
+
11
+ # @return [String]
12
+ attr_accessor :waiter_class_name
13
+
14
+ # @return [Integer]
15
+ attr_accessor :delay
16
+
17
+ # @return [Integer]
18
+ attr_accessor :max_attempts
19
+
20
+ # @return [String<Ruby>]
21
+ attr_accessor :wait_call
22
+
23
+ # @return [String<Ruby>]
24
+ attr_accessor :constructor_args
25
+
26
+ class << self
27
+
28
+ # @param [Hash] resource
29
+ # @param [Hash, nil] client_waiters
30
+ # @return [Array<ResourceWaiter>]
31
+ def build_list(resource, client_waiters)
32
+ client_waiters = Waiter.build_list(client_waiters)
33
+ (resource['waiters'] || {}).map do |name, waiter|
34
+ client_waiter = client_waiters.find do |w|
35
+ w.name == Underscore.underscore(waiter['waiterName'])
36
+ end
37
+ ResourceWaiter.new.tap do |w|
38
+ w.name = Underscore.underscore(name)
39
+ w.client_waiter_name = client_waiter.name
40
+ w.waiter_class_name = client_waiter.class_name
41
+ w.delay = client_waiter.delay
42
+ w.max_attempts = client_waiter.max_attempts
43
+ w.wait_call = wait_call(waiter)
44
+ w.constructor_args = constructor_args(resource, waiter)
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def wait_call(waiter)
52
+ args = ResourceClientRequestParams.new(
53
+ params: waiter['params']
54
+ ).to_s.strip
55
+ if waiter['path']
56
+ "resp = waiter.wait(params.merge(#{args}))"
57
+ else
58
+ "waiter.wait(params.merge(#{args}))"
59
+ end
60
+ end
61
+
62
+ def constructor_args(resource, waiter)
63
+ args = {}
64
+ (resource['identifiers'] || []).each do |i|
65
+ name = Underscore.underscore(i['name']).to_sym
66
+ args[name] = "@#{name}"
67
+ end
68
+ if waiter['path']
69
+ args[:data] = ResourceValueSource.new(
70
+ 'source' => 'response',
71
+ 'path' => waiter['path']
72
+ )
73
+ end
74
+ args[:client] = '@client'
75
+ HashFormatter.new(wrap: false).format(args)[1..-2]
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AwsSdkCodeGenerator
2
4
  class Service
3
5
 
@@ -12,6 +14,7 @@ module AwsSdkCodeGenerator
12
14
  # @option options [Hash, String] :waiters
13
15
  # @option options [Hash, String] :resources
14
16
  # @option options [Hash, String] :examples
17
+ # @option options [Hash, String] :smoke_tests
15
18
  # @option options [Hash<gem,version>] :gem_dependencies ({})
16
19
  # @option options [Hash] :add_plugins ({})
17
20
  # @option options [Hash] :remove_plugins ([])
@@ -22,24 +25,32 @@ module AwsSdkCodeGenerator
22
25
  @gem_name = options[:gem_name] || "aws-sdk-#{identifier}"
23
26
  @gem_version = options.fetch(:gem_version)
24
27
  @api = load_json(options.fetch(:api))
25
- ApplyDocs.new(@api).apply(load_json(options[:docs]))
28
+ unless @api['metadata']['protocol'] == 'api-gateway'
29
+ # Dont reply on API Gateway doc.json
30
+ ApplyDocs.new(@api).apply(load_json(options[:docs]))
31
+ end
26
32
  @paginators = load_json(options[:paginators])
27
33
  @waiters = load_json(options[:waiters])
28
34
  @resources = load_json(options[:resources])
29
35
  @examples = load_json(options[:examples])
30
- @gem_dependencies = {
31
- 'aws-sdk-core' => '3.0.0.rc1'
32
- }.merge(options[:gem_dependencies] || {})
36
+ @smoke_tests = load_json(options[:smoke_tests])
37
+ @gem_dependencies = options[:gem_dependencies] || {}
33
38
  @add_plugins = options[:add_plugins] || {}
34
39
  @remove_plugins = options[:remove_plugins] || []
35
40
  @endpoints_key = options.fetch(:endpoints_key, nil)
41
+ # APIG custom service only
42
+ @default_endpoint = options[:default_endpoint]
36
43
 
37
44
  # computed attributes
38
45
  @protocol = api.fetch('metadata').fetch('protocol')
46
+ @protocol_settings = api.fetch('metadata')['protocolSettings'] || {}
39
47
  @api_version = api.fetch('metadata')['apiVersion']
40
48
  @signature_version = api.fetch('metadata')['signatureVersion']
41
49
  @full_name = api.fetch('metadata')['serviceFullName']
42
50
  @short_name = api.fetch('metadata')['serviceAbbreviation'] || @full_name
51
+ @require_endpoint_discovery = api.fetch('operations', []).any? do |_, o|
52
+ o['endpointdiscovery'] && o['endpointdiscovery']['required']
53
+ end
43
54
  end
44
55
 
45
56
  # @return [String] The service name, e.g. "S3"
@@ -63,6 +74,9 @@ module AwsSdkCodeGenerator
63
74
  # @return [Hash, nil] The service shared examples model.
64
75
  attr_reader :examples
65
76
 
77
+ # @return [Hash, nil] The service smoke test model.
78
+ attr_reader :smoke_tests
79
+
66
80
  # @return [Hash<String,String>] A hash of gem dependencies. Hash keys
67
81
  # are gem names, values are versions.
68
82
  attr_reader :gem_dependencies
@@ -70,9 +84,15 @@ module AwsSdkCodeGenerator
70
84
  # @return [String]
71
85
  attr_reader :gem_version
72
86
 
87
+ # @return [String] The gem name, e.g. "aws-sdk-s3"
88
+ attr_reader :gem_name
89
+
73
90
  # @return [String, nil]
74
91
  attr_reader :endpoints_key
75
92
 
93
+ # @return [String] Required for APIG custom service
94
+ attr_reader :default_endpoint
95
+
76
96
  # @return [Hash<String,String>] A hash of plugins to add.
77
97
  attr_reader :add_plugins
78
98
 
@@ -82,12 +102,12 @@ module AwsSdkCodeGenerator
82
102
  # @return [String] The service identifier, e.g. "s3"
83
103
  attr_reader :identifier
84
104
 
85
- # @return [String] The gem name, e.g. "aws-sdk-s3"
86
- attr_reader :gem_name
87
-
88
105
  # @return [String] The service protocol, e.g. "json", "query", etc.
89
106
  attr_reader :protocol
90
107
 
108
+ # @return [Hash] The service protocol settings
109
+ attr_reader :protocol_settings
110
+
91
111
  # @return [String] The service API version, e.g. "YYYY-MM-DD".
92
112
  attr_reader :api_version
93
113
 
@@ -101,6 +121,9 @@ module AwsSdkCodeGenerator
101
121
  # @return [String] The short product name for the service, e.g. "Amazon S3".
102
122
  attr_reader :short_name
103
123
 
124
+ # @return [Boolean] true if any operation requires endpoint_discovery
125
+ attr_reader :require_endpoint_discovery
126
+
104
127
  # @api private
105
128
  def inspect
106
129
  "#<#{self.class.name}>"
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwsSdkCodeGenerator
4
+ class SharedExample
5
+
6
+ def initialize(params, method_name, operation, api, comments)
7
+ @params = params
8
+ @method_name = method_name
9
+ @operation = operation
10
+ @api = api
11
+ @comments = comments
12
+ @params ||= {}
13
+ end
14
+
15
+ def to_str_input
16
+ lines = structure(@params, Api.shape(@operation['input'], @api), "", [])
17
+ params = lines.join("\n")
18
+ "resp = client.#{@method_name}(#{params})"
19
+ end
20
+
21
+ def to_str_output
22
+ lines = structure(@params, Api.shape(@operation['output'], @api), "", [])
23
+ params = lines.join("\n")
24
+ "#{params}"
25
+ end
26
+
27
+ private
28
+
29
+ def entry(json, ref, indent, path)
30
+ case ref['type']
31
+ when 'structure'
32
+ if is_ddb_simple_attribute?(ref)
33
+ string(json.first[1])
34
+ else
35
+ structure(json, ref, indent, path)
36
+ end
37
+ when 'map' then map(json, ref, indent, path)
38
+ when 'list' then list(json, ref, indent, path)
39
+ when 'timestamp' then "Time.parse(#{json.inspect})"
40
+ when 'string', 'blob' then string(json)
41
+ else json
42
+ end
43
+ end
44
+
45
+ def is_ddb_simple_attribute?(ref)
46
+ ref['members'].keys == ["S", "N", "B", "SS", "NS", "BS", "M", "L", "NULL", "BOOL"] &&
47
+ @api['metadata']['serviceFullName'] == 'Amazon DynamoDB'
48
+ end
49
+
50
+ def string(json)
51
+ # Travis jruby hangs when parsing long string (e.g. policy string)
52
+ # Clean all white spaces in those strings
53
+ if json.length > 2048
54
+ json.gsub(/\s+/, "").inspect
55
+ else
56
+ json.inspect
57
+ end
58
+ end
59
+
60
+ def structure(json, ref, indent, path)
61
+ lines = ["{"]
62
+ json.each do |key, val|
63
+ path << ".#{key}"
64
+ shape_val = entry(val, Api.shape(ref['members'][key], @api), "#{indent} ", path)
65
+ lines << "#{indent} #{Underscore.underscore(key)}: #{format(shape_val)}, #{apply_comments(path)}"
66
+ path.pop
67
+ end
68
+ lines << "#{indent}}"
69
+ lines
70
+ end
71
+
72
+ def map(json, ref, indent, path)
73
+ lines = ["{"]
74
+ json.each do |key, val|
75
+ path << ".#{key}"
76
+ _, ref_value = Api.resolve(ref['value'], @api)
77
+ shape_val = entry(val, ref_value, "#{indent} ", path)
78
+ lines << "#{indent} \"#{key}\" => #{format(shape_val)}, #{apply_comments(path)}"
79
+ path.pop
80
+ end
81
+ lines << "#{indent}}"
82
+ lines
83
+ end
84
+
85
+ def list(json, ref, indent, path)
86
+ lines = ["["]
87
+ json.each_with_index do |member, index|
88
+ path << "[#{index}]"
89
+ _, ref_val = Api.resolve(ref['member']['shape'], @api)
90
+ shape_val = entry(member, ref_val, "#{indent} ", path)
91
+ if shape_val.is_a?(Array)
92
+ shape_val = shape_val.join("\n")
93
+ end
94
+ lines << "#{indent} #{shape_val}, #{apply_comments(path)}"
95
+ path.pop
96
+ end
97
+ lines << "#{indent}]"
98
+ lines
99
+ end
100
+
101
+ def format(shape_val)
102
+ formatted = []
103
+ if shape_val.is_a?(Array)
104
+ hashes = []
105
+ shape_val.each do |v|
106
+ hashes << (v.is_a?(Hash) ? format_hash(v, ' ') : v)
107
+ end
108
+ hashes.join(",")
109
+ formatted << hashes
110
+ elsif shape_val.is_a?(Hash)
111
+ formatted << format_hash(shape_val, ' ')
112
+ else
113
+ formatted << shape_val
114
+ end
115
+ formatted.join("\n")
116
+ end
117
+
118
+ def format_hash(value, indent)
119
+ HashFormatter.new(indent: indent).format(value)
120
+ end
121
+
122
+ def apply_comments(path)
123
+ key = path.join().sub(/^\./, '')
124
+ if @comments && @comments[key]
125
+ "# #{@comments[key]}"
126
+ else
127
+ ""
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ module AwsSdkCodeGenerator
6
+ class SyntaxExample
7
+
8
+ include Helper
9
+
10
+ # @option options [required, Hash] :api
11
+ # @option options [required, Hash] :shape
12
+ def initialize(options = {})
13
+ @api = options.fetch(:api)
14
+ @method_name = options.fetch(:method_name)
15
+ @receiver = options.fetch(:receiver)
16
+ @resp_var = options[:resp_var] ? "#{options[:resp_var]} = " : ''
17
+ @shape = filter_shape(options)
18
+ @async = options[:async] || false
19
+ @hash = SyntaxExampleHash.new(api: @api, shape: @shape, async: @async).format('# ') if @shape
20
+ end
21
+
22
+ def format
23
+ if @shape && @shape['members'] && @shape['members'].length > 0
24
+ <<-EXAMPLE.strip
25
+ # @example Request syntax with placeholder values
26
+ #
27
+ # #{@resp_var}#{@receiver}.#{@method_name}(#{@hash})
28
+ #{async_format}
29
+ EXAMPLE
30
+ else
31
+ nil
32
+ end
33
+ end
34
+
35
+ def async_format
36
+ if @async
37
+ <<-ASYNC.strip
38
+ # # => Seahorse::Client::AsyncResponse
39
+ # async_resp.wait
40
+ # # => Seahorse::Client::Response
41
+ # # Or use async_resp.join!
42
+ ASYNC
43
+ else
44
+ nil
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def filter_shape(options)
51
+ skip = Set.new(options.fetch(:skip, []))
52
+ shape = Helper.deep_copy(options.fetch(:shape))
53
+ options.fetch(:skip, []).each do |skip|
54
+ shape['members'].delete(skip)
55
+ end
56
+ shape
57
+ end
58
+
59
+ end
60
+ end