Skip to content

Commit 89ac402

Browse files
author
Matt Muller
committed
CBOR -> Cbor and deserialize/serialize -> parse/build
1 parent 15b4b35 commit 89ac402

27 files changed

Lines changed: 163 additions & 152 deletions

File tree

gems/smithy-cbor/lib/smithy-cbor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
require_relative 'smithy-cbor/encoder'
88

99
module Smithy
10-
# Smithy::CBOR is a purpose-built set of utilities for working with CBOR.
10+
# Smithy::Cbor is a purpose-built set of utilities for working with CBOR.
1111
# It does not support all features of generic CBOR parsing and serialization.
12-
module CBOR
12+
module Cbor
1313
VERSION = File.read(File.expand_path('../VERSION', __dir__.to_s)).strip
1414

1515
# CBOR Tagged data (Major type 6).

gems/smithy-cbor/lib/smithy-cbor/serializer.rb renamed to gems/smithy-cbor/lib/smithy-cbor/builder.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
require 'base64'
44

55
module Smithy
6-
module CBOR
6+
module Cbor
77
# @api private
8-
class Serializer
8+
class Builder
99
include Schema::Shapes
1010

1111
def initialize(options = {})
1212
@options = options
1313
end
1414

15-
def serialize(shape, data)
15+
def build(shape, data)
1616
ref = shape.is_a?(ShapeRef) ? shape : ShapeRef.new(shape: shape)
1717
return if ref.shape == Prelude::Unit
1818

19-
CBOR.encode(shape(ref, data))
19+
Cbor.encode(shape(ref, data))
2020
end
2121

2222
private

gems/smithy-cbor/lib/smithy-cbor/codec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# frozen_string_literal: true
22

3-
require_relative 'deserializer'
4-
require_relative 'serializer'
3+
require_relative 'builder'
4+
require_relative 'parser'
55

66
module Smithy
7-
module CBOR
8-
# Codec that serializes and deserializes in CBOR format.
7+
module Cbor
8+
# Codec that builds and parses in CBOR format.
99
class Codec
1010
# @param [Hash] options
1111
def initialize(options = {})
@@ -15,16 +15,16 @@ def initialize(options = {})
1515
# @param [Shape] shape
1616
# @param [Object] data
1717
# @return [String, nil]
18-
def serialize(shape, data)
19-
Serializer.new(@options).serialize(shape, data)
18+
def build(shape, data)
19+
Builder.new(@options).build(shape, data)
2020
end
2121

2222
# @param [Shape] shape
2323
# @param [String] bytes
2424
# @param [Object] target
2525
# @return [Object]
26-
def deserialize(shape, bytes, target = nil)
27-
Deserializer.new(@options).deserialize(shape, bytes, target)
26+
def parse(shape, bytes, target = nil)
27+
Parser.new(@options).parse(shape, bytes, target)
2828
end
2929
end
3030
end

gems/smithy-cbor/lib/smithy-cbor/decoder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'bigdecimal'
44

55
module Smithy
6-
module CBOR
6+
module Cbor
77
# @api private
88
class Decoder # rubocop:disable Metrics/ClassLength
99
FIVE_BIT_MASK = 0x1F

gems/smithy-cbor/lib/smithy-cbor/encoder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'bigdecimal'
44

55
module Smithy
6-
module CBOR
6+
module Cbor
77
# @api private
88
class Encoder
99
MAJOR_TYPE_UNSIGNED_INT = 0x00 # 000_00000 - Major Type 0 - unsigned int

gems/smithy-cbor/lib/smithy-cbor/deserializer.rb renamed to gems/smithy-cbor/lib/smithy-cbor/parser.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
require 'base64'
44

55
module Smithy
6-
module CBOR
6+
module Cbor
77
# @api private
8-
class Deserializer
8+
class Parser
99
include Schema::Shapes
1010

1111
def initialize(options = {})
1212
@options = options
1313
end
1414

15-
def deserialize(shape, bytes, target)
15+
def parse(shape, bytes, target)
1616
return {} if bytes.empty?
1717

1818
ref = shape.is_a?(ShapeRef) ? shape : ShapeRef.new(shape: shape)
19-
shape(ref, CBOR.decode(bytes), target)
19+
shape(ref, Cbor.decode(bytes), target)
2020
end
2121

2222
private
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Smithy
2-
module CBOR
2+
module Cbor
33
module Codec
4-
def serialize: (Schema::Shapes::Shape shape, untyped data) -> (nil | String)
5-
def deserialize: (Schema::Shapes::Shape shape, String bytes, ?Struct[untyped] target) -> (Hash[untyped, untyped] | ::Struct[untyped] | Schema::Union)
4+
def build: (Schema::Shapes::Shape shape, untyped data) -> (nil | String)
5+
def parse: (Schema::Shapes::Shape shape, String bytes, ?Struct[untyped] target) -> (Hash[untyped, untyped] | ::Struct[untyped] | Schema::Union)
66
end
77
end
88
end

gems/smithy-cbor/spec/smithy-cbor/codec_spec.rb

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
require_relative '../spec_helper'
44

55
module Smithy
6-
module CBOR
6+
module Cbor
77
describe Codec do
88
let(:structure_shape) { SchemaHelper.sample_schema.const_get(:Structure) }
99

10-
it 'serialize returns nil when given a unit shape' do
11-
expect(subject.serialize(Schema::Shapes::Prelude::Unit, '')).to be_nil
10+
it 'build returns nil when given a unit shape' do
11+
expect(subject.build(Schema::Shapes::Prelude::Unit, '')).to be_nil
1212
end
1313

14-
it 'deserializes returns an empty hash when given bytes are empty' do
15-
expect(subject.deserialize(Schema::Shapes::Prelude::String, '')).to be_empty
14+
it 'parses returns an empty hash when given bytes are empty' do
15+
expect(subject.parse(Schema::Shapes::Prelude::String, '')).to be_empty
1616
end
1717

18-
it 'deserializes returns an empty hash when given a unit shape' do
19-
expect(subject.deserialize(Schema::Shapes::Prelude::Unit, '')).to be_empty
18+
it 'parses returns an empty hash when given a unit shape' do
19+
expect(subject.parse(Schema::Shapes::Prelude::Unit, '')).to be_empty
2020
end
2121

22-
it 'serializes and deserializes data' do
22+
it 'builds and parses data' do
2323
time = Time.now
2424
allow(Time).to receive(:at).and_return(time)
2525
data = {
@@ -45,95 +45,95 @@ module CBOR
4545
union: { string: 'string' }
4646
}
4747
data = data.merge(structure: data)
48-
bytes = subject.serialize(structure_shape, data)
49-
expect(subject.deserialize(structure_shape, bytes).to_h).to eq(data)
48+
bytes = subject.build(structure_shape, data)
49+
expect(subject.parse(structure_shape, bytes).to_h).to eq(data)
5050
end
5151

5252
context 'structures' do
53-
it 'serializes and deserializes structures as a type' do
53+
it 'builds and parses structures as a type' do
5454
type = structure_shape.type.new(string: 'string')
55-
bytes = subject.serialize(structure_shape, type)
56-
expect(subject.deserialize(structure_shape, bytes).string).to eq('string')
55+
bytes = subject.build(structure_shape, type)
56+
expect(subject.parse(structure_shape, bytes).string).to eq('string')
5757
end
5858

59-
it 'serializes and deserializes structures as a hash' do
59+
it 'builds and parses structures as a hash' do
6060
data = { string: 'string' }
61-
bytes = subject.serialize(structure_shape, data)
62-
expect(subject.deserialize(structure_shape, bytes).to_h).to eq(data)
61+
bytes = subject.build(structure_shape, data)
62+
expect(subject.parse(structure_shape, bytes).to_h).to eq(data)
6363
end
6464
end
6565

6666
context 'unions' do
67-
it 'serializes and deserializes union as a type' do
67+
it 'builds and parses union as a type' do
6868
union = structure_shape.member(:union).shape.member_type(:string).new(string: 'string')
6969
type = structure_shape.type.new(union: union)
70-
bytes = subject.serialize(structure_shape, type)
71-
expect(subject.deserialize(structure_shape, bytes).union).to eq(union)
70+
bytes = subject.build(structure_shape, type)
71+
expect(subject.parse(structure_shape, bytes).union).to eq(union)
7272
end
7373

74-
it 'serializes and deserializes union as a hash' do
74+
it 'builds and parses union as a hash' do
7575
data = { union: { string: 'string' } }
76-
bytes = subject.serialize(structure_shape, data)
77-
expect(subject.deserialize(structure_shape, bytes).to_h).to eq(data)
76+
bytes = subject.build(structure_shape, data)
77+
expect(subject.parse(structure_shape, bytes).to_h).to eq(data)
7878
end
7979

80-
it 'serializes and deserializes unit members as a type' do
80+
it 'builds and parses unit members as a type' do
8181
union = structure_shape.member(:union).shape.member_type(:unit).new(unit: Schema::EmptyStructure.new)
8282
type = structure_shape.type.new(union: union)
83-
bytes = subject.serialize(structure_shape, type)
84-
expect(subject.deserialize(structure_shape, bytes).union).to eq(union)
83+
bytes = subject.build(structure_shape, type)
84+
expect(subject.parse(structure_shape, bytes).union).to eq(union)
8585
end
8686

87-
it 'serializes and deserializes unit members as a hash' do
87+
it 'builds and parses unit members as a hash' do
8888
data = { union: { unit: {} } }
89-
bytes = subject.serialize(structure_shape, data)
90-
expect(subject.deserialize(structure_shape, bytes).to_h).to eq(data)
89+
bytes = subject.build(structure_shape, data)
90+
expect(subject.parse(structure_shape, bytes).to_h).to eq(data)
9191
end
9292

93-
it 'serializes a nil union' do
93+
it 'builds a nil union' do
9494
data = { union: nil }
95-
bytes = subject.serialize(structure_shape, data)
96-
expect(subject.deserialize(structure_shape, bytes).union).to eq(nil)
95+
bytes = subject.build(structure_shape, data)
96+
expect(subject.parse(structure_shape, bytes).union).to eq(nil)
9797
end
9898

99-
it 'deserializes unknown union members' do
99+
it 'parses unknown union members' do
100100
unknown_union_type = structure_shape.member(:union).shape.member_type(:unknown)
101101
data = { union: { 'someThing' => 'someValue' } }
102-
deserialized = subject.deserialize(structure_shape, CBOR.encode(data))
103-
expect(deserialized.union).to be_a(unknown_union_type)
104-
expect(deserialized.union.to_h).to eq(unknown: { 'someThing' => 'someValue' })
102+
parsed = subject.parse(structure_shape, Cbor.encode(data))
103+
expect(parsed.union).to be_a(unknown_union_type)
104+
expect(parsed.union.to_h).to eq(unknown: { 'someThing' => 'someValue' })
105105
end
106106
end
107107

108108
context 'lists' do
109-
it 'serializes and deserializes lists' do
109+
it 'builds and parses lists' do
110110
data = { list: ['string'] }
111-
bytes = subject.serialize(structure_shape, data)
112-
expect(subject.deserialize(structure_shape, bytes).to_h).to eq(data)
111+
bytes = subject.build(structure_shape, data)
112+
expect(subject.parse(structure_shape, bytes).to_h).to eq(data)
113113
end
114114

115115
it 'can handle sparse lists' do
116116
list_shape = structure_shape.member(:list).shape
117117
list_shape.traits.merge!('smithy.api#sparse' => {})
118118
data = { list: [nil] }
119-
bytes = subject.serialize(structure_shape, data)
120-
expect(subject.deserialize(structure_shape, bytes).to_h).to eq(data)
119+
bytes = subject.build(structure_shape, data)
120+
expect(subject.parse(structure_shape, bytes).to_h).to eq(data)
121121
end
122122
end
123123

124124
context 'maps' do
125-
it 'serializes and deserializes maps' do
125+
it 'builds and parses maps' do
126126
data = { map: { 'key' => 'value' } }
127-
bytes = subject.serialize(structure_shape, data)
128-
expect(subject.deserialize(structure_shape, bytes).to_h).to eq(data)
127+
bytes = subject.build(structure_shape, data)
128+
expect(subject.parse(structure_shape, bytes).to_h).to eq(data)
129129
end
130130

131131
it 'can handle sparse maps' do
132132
map_shape = structure_shape.member(:map).shape
133133
map_shape.traits.merge!('smithy.api#sparse' => {})
134134
data = { map: { 'key' => nil } }
135-
bytes = subject.serialize(structure_shape, data)
136-
expect(subject.deserialize(structure_shape, bytes).to_h).to eq(data)
135+
bytes = subject.build(structure_shape, data)
136+
expect(subject.parse(structure_shape, bytes).to_h).to eq(data)
137137
end
138138
end
139139
end

gems/smithy-cbor/spec/smithy-cbor/decoder_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_relative '../spec_helper'
44

55
module Smithy
6-
module CBOR
6+
module Cbor
77
# covers cases not included in test suite from cbor_spec
88
describe Decoder do
99
def cbor64_decode(value)

gems/smithy-cbor/spec/smithy-cbor/encoder_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_relative '../spec_helper'
44

55
module Smithy
6-
module CBOR
6+
module Cbor
77
describe Encoder do
88
let(:time) { Time.parse('2020-01-01 12:21:42Z') }
99

0 commit comments

Comments
 (0)