functions_framework 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,173 +0,0 @@
1
- # Copyright 2020 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- require "base64"
16
- require "json"
17
-
18
- module FunctionsFramework
19
- module CloudEvents
20
- ##
21
- # An implementation of JSON format and JSON batch format.
22
- #
23
- # Supports the CloudEvents 0.3 and CloudEvents 1.0 variants of this format.
24
- # See https://github.com/cloudevents/spec/blob/v0.3/json-format.md and
25
- # https://github.com/cloudevents/spec/blob/v1.0/json-format.md.
26
- #
27
- class JsonFormat
28
- ##
29
- # Decode an event from the given input JSON string.
30
- #
31
- # @param json [String] A JSON-formatted string
32
- # @return [FunctionsFramework::CloudEvents::Event]
33
- #
34
- def decode json, **_other_kwargs
35
- structure = ::JSON.parse json
36
- decode_hash_structure structure
37
- end
38
-
39
- ##
40
- # Encode an event to a JSON string.
41
- #
42
- # @param event [FunctionsFramework::CloudEvents::Event] An input event.
43
- # @param sort [boolean] Whether to sort keys of the JSON output.
44
- # @return [String] The JSON representation.
45
- #
46
- def encode event, sort: false, **_other_kwargs
47
- structure = encode_hash_structure event
48
- structure = sort_keys structure if sort
49
- ::JSON.dump structure
50
- end
51
-
52
- ##
53
- # Decode a batch of events from the given input string.
54
- #
55
- # @param json [String] A JSON-formatted string
56
- # @return [Array<FunctionsFramework::CloudEvents::Event>]
57
- #
58
- def decode_batch json, **_other_kwargs
59
- structure_array = Array(::JSON.parse(json))
60
- structure_array.map do |structure|
61
- decode_hash_structure structure
62
- end
63
- end
64
-
65
- ##
66
- # Encode a batch of event to a JSON string.
67
- #
68
- # @param events [Array<FunctionsFramework::CloudEvents::Event>] An array
69
- # of input events.
70
- # @param sort [boolean] Whether to sort keys of the JSON output.
71
- # @return [String] The JSON representation.
72
- #
73
- def encode_batch events, sort: false, **_other_kwargs
74
- structure_array = Array(events).map do |event|
75
- structure = encode_hash_structure event
76
- sort ? sort_keys(structure) : structure
77
- end
78
- ::JSON.dump structure_array
79
- end
80
-
81
- ##
82
- # Decode a single event from a hash data structure with keys and types
83
- # conforming to the JSON envelope.
84
- #
85
- # @param structure [Hash] An input hash.
86
- # @return [FunctionsFramework::CloudEvents::Event]
87
- #
88
- def decode_hash_structure structure
89
- spec_version = structure["specversion"].to_s
90
- case spec_version
91
- when "0.3"
92
- decode_hash_structure_v0 structure
93
- when /^1(\.|$)/
94
- decode_hash_structure_v1 structure
95
- else
96
- raise SpecVersionError, "Unrecognized specversion: #{spec_version}"
97
- end
98
- end
99
-
100
- ##
101
- # Encode a single event to a hash data structure with keys and types
102
- # conforming to the JSON envelope.
103
- #
104
- # @param event [FunctionsFramework::CloudEvents::Event] An input event.
105
- # @return [String] The hash structure.
106
- #
107
- def encode_hash_structure event
108
- case event
109
- when Event::V0
110
- encode_hash_structure_v0 event
111
- when Event::V1
112
- encode_hash_structure_v1 event
113
- else
114
- raise SpecVersionError, "Unrecognized specversion: #{event.spec_version}"
115
- end
116
- end
117
-
118
- private
119
-
120
- def sort_keys hash
121
- result = {}
122
- hash.keys.sort.each do |key|
123
- result[key] = hash[key]
124
- end
125
- result
126
- end
127
-
128
- def decode_hash_structure_v0 structure
129
- data = structure["data"]
130
- content_type = structure["datacontenttype"]
131
- if data.is_a?(::String) && content_type.is_a?(::String)
132
- content_type = ContentType.new content_type
133
- if content_type.subtype == "json" || content_type.subtype_format == "json"
134
- structure = structure.dup
135
- structure["data"] = ::JSON.parse data rescue data
136
- structure["datacontenttype"] = content_type
137
- end
138
- end
139
- Event::V0.new attributes: structure
140
- end
141
-
142
- def decode_hash_structure_v1 structure
143
- if structure.key? "data_base64"
144
- structure = structure.dup
145
- structure["data"] = ::Base64.decode64 structure.delete "data_base64"
146
- end
147
- Event::V1.new attributes: structure
148
- end
149
-
150
- def encode_hash_structure_v0 event
151
- structure = event.to_h
152
- data = event.data
153
- content_type = event.data_content_type
154
- if data.is_a?(::String) && !content_type.nil?
155
- if content_type.subtype == "json" || content_type.subtype_format == "json"
156
- structure["data"] = ::JSON.parse data rescue data
157
- end
158
- end
159
- structure
160
- end
161
-
162
- def encode_hash_structure_v1 event
163
- structure = event.to_h
164
- data = structure["data"]
165
- if data.is_a?(::String) && data.encoding == ::Encoding::ASCII_8BIT
166
- structure.delete "data"
167
- structure["data_base64"] = ::Base64.encode64 data
168
- end
169
- structure
170
- end
171
- end
172
- end
173
- end