smithy-schema 1.0.0.pre0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2dc2a4e23db98951101c73d858d031c3a1e2b757c49d891c448bd3e049a6917d
4
+ data.tar.gz: 888f46534f8c174d00f6a09470ce69ac183ab68dd9d9c43e55f9e4ec51b3cf07
5
+ SHA512:
6
+ metadata.gz: 949146a02a4357e994ad85028d06cb198bb20dbb208c5d8d001762ff7653b7c9c03ca2f7aa313aa85f2b6051798184dc24b3c2547a9e034cab818c3bb6b0edef
7
+ data.tar.gz: f63565e8f4297c5c9e20720d89ac5f316f8ac590572f4d600196d2aa15cc82c9720496c26559a300806d1346bb1cd611ee00f7caa06047c96b60b8add5a8676c
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ Unreleased Changes
2
+ ------------------
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0.pre0
@@ -0,0 +1,327 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smithy
4
+ module Schema
5
+ # Represents shape types from the Smithy model.
6
+ module Shapes
7
+ # A base shape that all shapes inherits from.
8
+ class Shape
9
+ def initialize(options = {})
10
+ @id = options[:id]
11
+ @traits = options[:traits] || {}
12
+ end
13
+
14
+ # @return [String, nil] Absolute shape ID from model
15
+ attr_accessor :id
16
+
17
+ # @return [Hash<String, Object>]
18
+ attr_accessor :traits
19
+ end
20
+
21
+ # Represents an aggregate shape that has members.
22
+ class Structure < Shape
23
+ def initialize(options = {})
24
+ super
25
+ @members = {}
26
+ @names_by_member_name = {}
27
+ end
28
+
29
+ # @return [Hash<Symbol, MemberShape>]
30
+ attr_accessor :members
31
+
32
+ # @return [Hash<String, Symbol>]
33
+ attr_accessor :names_by_member_name
34
+
35
+ # @return [Class, nil]
36
+ attr_accessor :type
37
+
38
+ # @return [MemberShape]
39
+ def add_member(name, member_name, shape, traits: {})
40
+ @names_by_member_name[member_name] = name
41
+ @members[name] = MemberShape.new(member_name, shape, traits: traits)
42
+ end
43
+
44
+ # @param [Symbol] name
45
+ # @return [Boolean]
46
+ def member?(name)
47
+ @members.key?(name)
48
+ end
49
+
50
+ # @param [Symbol] name
51
+ # @return [MemberShape, nil]
52
+ def member(name)
53
+ @members[name]
54
+ end
55
+
56
+ # @param [String] member_name
57
+ # @return [Boolean]
58
+ def name_by_member_name?(member_name)
59
+ @names_by_member_name.key?(member_name)
60
+ end
61
+
62
+ # @param [String] member_name
63
+ # @return [Symbol, nil]
64
+ def name_by_member_name(member_name)
65
+ @names_by_member_name[member_name]
66
+ end
67
+ end
68
+
69
+ # Represents a slim variation of the Service shape.
70
+ class ServiceShape < Shape
71
+ include Enumerable
72
+
73
+ def initialize(options = {})
74
+ super
75
+ @name = options[:name]
76
+ @version = options[:version]
77
+ @operations = {}
78
+ yield self if block_given?
79
+ end
80
+
81
+ # @return [String, nil] Service name
82
+ attr_accessor :name
83
+
84
+ # @return [String, nil]
85
+ attr_accessor :version
86
+
87
+ # @return [Hash<Symbol, OperationShape>]
88
+ attr_accessor :operations
89
+
90
+ # @return [Hash<Symbol, OperationShape>]
91
+ def each(&)
92
+ @operations.each(&)
93
+ end
94
+
95
+ # @return [OperationShape]
96
+ def add_operation(name, operation)
97
+ @operations[name] = operation
98
+ end
99
+
100
+ # @param [Symbol] name
101
+ # @return [OperationShape] operation
102
+ def operation(name)
103
+ raise ArgumentError, "unknown operation #{name.inspect}" unless @operations.key?(name)
104
+
105
+ @operations[name]
106
+ end
107
+
108
+ # @return [Array<Symbol>]
109
+ def operation_names
110
+ @operations.keys
111
+ end
112
+ end
113
+
114
+ # Represents an Operation shape.
115
+ class OperationShape < Shape
116
+ def initialize(options = {})
117
+ super
118
+ @name = options[:name]
119
+ @input = options[:input]
120
+ @output = options[:output]
121
+ @errors = options[:errors] || []
122
+ yield self if block_given?
123
+ end
124
+
125
+ # @return [String, nil] Operation name
126
+ attr_accessor :name
127
+
128
+ # @return [StructureShape, nil]
129
+ attr_accessor :input
130
+
131
+ # @return [StructureShape, nil]
132
+ attr_accessor :output
133
+
134
+ # @return [Array<StructureShape>]
135
+ attr_accessor :errors
136
+ end
137
+
138
+ # Represents a BigDecimal shape.
139
+ class BigDecimalShape < Shape; end
140
+
141
+ # Represents both Blob and Data Stream shapes.
142
+ class BlobShape < Shape; end
143
+
144
+ # Represents a Boolean shape.
145
+ class BooleanShape < Shape; end
146
+
147
+ # Represents a Document shape.
148
+ class DocumentShape < Shape; end
149
+
150
+ # Represents an Enum shape.
151
+ class EnumShape < Structure; end
152
+
153
+ # Represents the following shapes: Byte, Short, Integer, Long, BigInteger.
154
+ class IntegerShape < Shape; end
155
+
156
+ # Represents an IntEnum shape.
157
+ class IntEnumShape < Structure; end
158
+
159
+ # Represents both Float and Double shapes.
160
+ class FloatShape < Shape; end
161
+
162
+ # Represents a List shape.
163
+ class ListShape < Shape
164
+ def initialize(options = {})
165
+ super
166
+ @member = nil
167
+ end
168
+
169
+ # @return [MemberShape, nil]
170
+ attr_accessor :member
171
+
172
+ def set_member(shape, traits: {})
173
+ @member = MemberShape.new('member', shape, traits: traits)
174
+ end
175
+ end
176
+
177
+ # Represents a Map shape.
178
+ class MapShape < Shape
179
+ def initialize(options = {})
180
+ super
181
+ @key = nil
182
+ @value = nil
183
+ end
184
+
185
+ # @return [MemberShape, nil]
186
+ attr_accessor :key
187
+
188
+ # @return [MemberShape, nil]
189
+ attr_accessor :value
190
+
191
+ def set_key(shape, traits: {})
192
+ @key = MemberShape.new('key', shape, traits: traits)
193
+ end
194
+
195
+ def set_value(shape, traits: {})
196
+ @value = MemberShape.new('value', shape, traits: traits)
197
+ end
198
+ end
199
+
200
+ # Represents a String shape.
201
+ class StringShape < Shape; end
202
+
203
+ # Represents a Structure shape.
204
+ class StructureShape < Structure
205
+ end
206
+
207
+ # Represents a Timestamp shape.
208
+ class TimestampShape < Shape; end
209
+
210
+ # Represents both Union and EventStream shapes.
211
+ class UnionShape < Structure
212
+ def initialize(options = {})
213
+ super
214
+ @member_types = {}
215
+ @members_by_type = {}
216
+ end
217
+
218
+ # @return [Hash<Symbol, Class>]
219
+ attr_accessor :member_types
220
+
221
+ # @return [Hash<Class, MemberShape>]
222
+ attr_accessor :members_by_type
223
+
224
+ # @return [MemberShape]
225
+ def add_member(name, member_name, shape, type, traits: {})
226
+ member = MemberShape.new(member_name, shape, traits: traits)
227
+ @members[name] = member
228
+ @names_by_member_name[member_name] = name
229
+ @member_types[name] = type
230
+ @members_by_type[type] = member
231
+ member
232
+ end
233
+
234
+ # @param [Symbol] name
235
+ # @return [Boolean]
236
+ def member_type?(name)
237
+ @member_types.key?(name)
238
+ end
239
+
240
+ # @param [Symbol] name
241
+ # @return [Class, nil]
242
+ def member_type(name)
243
+ @member_types[name]
244
+ end
245
+
246
+ # @param [Class] type
247
+ # @return [Boolean]
248
+ def member_by_type?(type)
249
+ @members_by_type.key?(type)
250
+ end
251
+
252
+ # @param [Class] type
253
+ # @return [MemberShape, nil]
254
+ def member_by_type(type)
255
+ @members_by_type[type]
256
+ end
257
+ end
258
+
259
+ # Represents a member shape.
260
+ class MemberShape
261
+ def initialize(name, shape, traits: {})
262
+ @name = name
263
+ @shape = shape
264
+ @traits = traits
265
+ end
266
+
267
+ # @return [String] Member name
268
+ attr_accessor :name
269
+
270
+ # @return [Shape] Referenced shape
271
+ attr_accessor :shape
272
+
273
+ # @return [Hash<String, Object>]
274
+ attr_accessor :traits
275
+ end
276
+
277
+ # Prelude shape definitions.
278
+ module Prelude
279
+ BigDecimal = BigDecimalShape.new(id: 'smithy.api#BigDecimal')
280
+ BigInteger = IntegerShape.new(id: 'smithy.api#BigInteger')
281
+ Blob = BlobShape.new(id: 'smithy.api#Blob')
282
+ Boolean = BooleanShape.new(id: 'smithy.api#Boolean')
283
+ Byte = IntegerShape.new(id: 'smithy.api#Byte')
284
+ Document = DocumentShape.new(id: 'smithy.api#Document')
285
+ Double = FloatShape.new(id: 'smithy.api#Double')
286
+ Float = FloatShape.new(id: 'smithy.api#Float')
287
+ Integer = IntegerShape.new(id: 'smithy.api#Integer')
288
+ Long = IntegerShape.new(id: 'smithy.api#Long')
289
+ PrimitiveBoolean = BooleanShape.new(
290
+ id: 'smithy.api#PrimitiveBoolean',
291
+ traits: { 'smithy.api#default' => false }
292
+ )
293
+ PrimitiveByte = IntegerShape.new(
294
+ id: 'smithy.api#PrimitiveByte',
295
+ traits: { 'smithy.api#default' => 0 }
296
+ )
297
+ PrimitiveDouble = FloatShape.new(
298
+ id: 'smithy.api#PrimitiveDouble',
299
+ traits: { 'smithy.api#default' => 0 }
300
+ )
301
+ PrimitiveFloat = FloatShape.new(
302
+ id: 'smithy.api#PrimitiveFloat',
303
+ traits: { 'smithy.api#default' => 0 }
304
+ )
305
+ PrimitiveInteger = IntegerShape.new(
306
+ id: 'smithy.api#PrimitiveInteger',
307
+ traits: { 'smithy.api#default' => 0 }
308
+ )
309
+ PrimitiveShort = IntegerShape.new(
310
+ id: 'smithy.api#PrimitiveShort',
311
+ traits: { 'smithy.api#default' => 0 }
312
+ )
313
+ PrimitiveLong = IntegerShape.new(
314
+ id: 'smithy.api#PrimitiveLong',
315
+ traits: { 'smithy.api#default' => 0 }
316
+ )
317
+ Short = IntegerShape.new(id: 'smithy.api#Short')
318
+ String = StringShape.new(id: 'smithy.api#String')
319
+ Timestamp = TimestampShape.new(id: 'smithy.api#Timestamp')
320
+ Unit = StructureShape.new(
321
+ id: 'smithy.api#Unit',
322
+ traits: { 'smithy.api#unitType' => {} }
323
+ )
324
+ end
325
+ end
326
+ end
327
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smithy
4
+ module Schema
5
+ # A module mixed into Structs that provides utility methods.
6
+ module Structure
7
+ # Deeply converts the Struct into a hash. Structure members that
8
+ # are `nil` are omitted from the resultant hash.
9
+ # @return [Hash, Structure]
10
+ def to_h(obj = self)
11
+ case obj
12
+ when Union
13
+ obj.to_h
14
+ when Structure
15
+ _to_h_structure(obj)
16
+ when Hash
17
+ _to_h_hash(obj)
18
+ when Array
19
+ _to_h_array(obj)
20
+ else
21
+ obj
22
+ end
23
+ end
24
+ alias to_hash to_h
25
+
26
+ private
27
+
28
+ def _to_h_structure(obj)
29
+ obj.members.each_with_object({}) do |member, hash|
30
+ value = obj.send(member)
31
+ hash[member] = to_hash(value) unless value.nil?
32
+ end
33
+ end
34
+
35
+ def _to_h_hash(obj)
36
+ obj.transform_values do |value|
37
+ to_hash(value)
38
+ end
39
+ end
40
+
41
+ def _to_h_array(obj)
42
+ obj.collect { |value| to_hash(value) }
43
+ end
44
+ end
45
+
46
+ # An empty Struct that includes the {Client::Structure} module.
47
+ EmptyStructure = Struct.new do
48
+ include Smithy::Schema::Structure
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'delegate'
4
+
5
+ module Smithy
6
+ module Schema
7
+ # Top level class for all generated Union types
8
+ class Union < ::SimpleDelegator
9
+ include Structure
10
+
11
+ def to_s
12
+ "#<#{self.class.name} #{__getobj__ || 'nil'}>"
13
+ end
14
+
15
+ def value
16
+ __getobj__
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'smithy-schema/shapes'
4
+ require_relative 'smithy-schema/structure'
5
+ require_relative 'smithy-schema/union'
6
+
7
+ module Smithy
8
+ # Base module for Smithy model classes.
9
+ module Model
10
+ VERSION = File.read(File.expand_path('../VERSION', __dir__.to_s)).strip
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smithy-schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre0
5
+ platform: ruby
6
+ authors:
7
+ - Amazon Web Services
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-03-06 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Smithy is a code generation toolkit for creating Client and Server SDKs
13
+ from Smithy models.
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - CHANGELOG.md
19
+ - VERSION
20
+ - lib/smithy-schema.rb
21
+ - lib/smithy-schema/shapes.rb
22
+ - lib/smithy-schema/structure.rb
23
+ - lib/smithy-schema/union.rb
24
+ homepage: https://github.com/smithy-lang/smithy-ruby
25
+ licenses:
26
+ - Apache-2.0
27
+ metadata: {}
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '3.3'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.6.2
43
+ specification_version: 4
44
+ summary: Schema definitions for Smithy generated clients and servers
45
+ test_files: []