Skip to content

Commit

Permalink
Add UUID format support (#1606)
Browse files Browse the repository at this point in the history
* Parsing uuid

* update generator

* update license

* add uuid runtime dependency

* pre-commit fixes

---------

Co-authored-by: api-clients-generation-pipeline[bot] <54105614+api-clients-generation-pipeline[bot]@users.noreply.github.com>
Co-authored-by: ci.datadog-api-spec <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2023
1 parent d004f34 commit 1df8e75
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .generator/src/generator/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,15 @@ def format_boolean(x):
raise TypeError(f"{x} is not supported type {schema}")
return "true" if x else "false"

def format_uuid(x):
return f'UUIDTools::UUID.parse("{x}")'

formatter = {
"number": format_number,
"integer": format_number,
"boolean": format_boolean,
"string": format_string,
"uuid": format_uuid,
None: repr,
}[schema.get("type")]

Expand Down
2 changes: 2 additions & 0 deletions .generator/src/generator/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def type_to_ruby(schema, alternative_name=None):
return "Time"
elif format_ == "binary":
return "File"
elif format_ == "uuid":
return "UUID"
return "String"
elif type_ == "boolean":
return "Boolean"
Expand Down
3 changes: 3 additions & 0 deletions .generator/src/generator/templates/api_client.j2
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ module {{ module_name }}
when 'Object'
# generic object (usually a Hash), return directly
data
when 'UUID'
# parse uuid object
data.to_s
when /\AArray<(.+)>\z/
# e.g. Array<Pet>
sub_type = $1
Expand Down
4 changes: 4 additions & 0 deletions .generator/src/generator/templates/model_base.j2
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ module {{ module_name }}::{{ version|upper }}
when :Array
# generic array, return directly
value
when :UUID
value.to_s
when /\AArray<(?<inner_type>.+)>\z/
inner_type = Regexp.last_match[:inner_type]
value.map { |v| _deserialize(inner_type, v) }
Expand Down Expand Up @@ -236,6 +238,8 @@ module {{ module_name }}::{{ version|upper }}
return data if data.instance_of?(String)
when 'Object' # "type: object"
return data if data.instance_of?(Hash)
when 'UUID'
return UUIDTools::UUID.parse(data)
when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
if data.instance_of?(Array)
sub_type = Regexp.last_match[:sub_type]
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ group :development, :test do
gem 'webmock'
gem 'zstandard'
gem 'pry'
gem 'uuidtools'
end

group :docs do
Expand Down
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ tomlrb,MIT,Francois Bernier
typhoeus,MIT,"David Balatero, Paul Dix, Hans Hasselberg"
tzinfo,MIT,Philip Ross
unicode-display_width,MIT,Jan Lelis
uuidtools,Apache-2.0,Bob Aman
vcr,"Hippocratic-2.1,MIT","Myron Marston, Kurtis Rainbolt-Greene, Olle Jonsson"
webmock,MIT,Bartosz Blimke
with_env,MIT,Zach Dennis
Expand Down
2 changes: 2 additions & 0 deletions datadog_api_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency "zeitwerk", "~> 2.6", ">= 2.6.0"

spec.add_runtime_dependency "uuidtools", "~> 2.1.5", ">= 2.1.5"

spec.add_development_dependency "rspec", "~> 3.6", ">= 3.6.0"

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|cassettes)/}) }
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog_api_client/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ def convert_to_type(data, return_type, api_version)
when 'Object'
# generic object (usually a Hash), return directly
data
when 'UUID'
# parse uuid object
data.to_s
when /\AArray<(.+)>\z/
# e.g. Array<Pet>
sub_type = $1
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog_api_client/v1/model_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def _deserialize(type, value)
when :Array
# generic array, return directly
value
when :UUID
value.to_s
when /\AArray<(?<inner_type>.+)>\z/
inner_type = Regexp.last_match[:inner_type]
value.map { |v| _deserialize(inner_type, v) }
Expand Down Expand Up @@ -247,6 +249,8 @@ def find_and_cast_into_type(klass, data)
return data if data.instance_of?(String)
when 'Object' # "type: object"
return data if data.instance_of?(Hash)
when 'UUID'
return UUIDTools::UUID.parse(data)
when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
if data.instance_of?(Array)
sub_type = Regexp.last_match[:sub_type]
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog_api_client/v2/model_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def _deserialize(type, value)
when :Array
# generic array, return directly
value
when :UUID
value.to_s
when /\AArray<(?<inner_type>.+)>\z/
inner_type = Regexp.last_match[:inner_type]
value.map { |v| _deserialize(inner_type, v) }
Expand Down Expand Up @@ -247,6 +249,8 @@ def find_and_cast_into_type(klass, data)
return data if data.instance_of?(String)
when 'Object' # "type: object"
return data if data.instance_of?(Hash)
when 'UUID'
return UUIDTools::UUID.parse(data)
when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
if data.instance_of?(Array)
sub_type = Regexp.last_match[:sub_type]
Expand Down

0 comments on commit 1df8e75

Please sign in to comment.