From 15acc38823a5dcd80f97ada634bd4e77d9948681 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Wed, 2 Apr 2025 00:31:42 +0000 Subject: [PATCH] feat: implement `to_json` for base model --- .rubocop.yml | 3 +-- lib/openai/type/base_model.rb | 10 ++++++++++ lib/openai/util.rb | 16 ++++++---------- rbi/lib/openai/type/base_model.rbi | 8 ++++++++ rbi/lib/openai/util.rbi | 4 ++-- sig/openai/type/base_model.rbs | 4 ++++ sig/openai/util.rbs | 4 ++-- test/openai/util_test.rb | 2 +- 8 files changed, 34 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index df60b4df..d66784a3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -127,8 +127,7 @@ Naming/ClassAndModuleCamelCase: - "**/*.rbi" Naming/MethodParameterName: - Exclude: - - "**/*.rbi" + Enabled: false Naming/PredicateName: Exclude: diff --git a/lib/openai/type/base_model.rb b/lib/openai/type/base_model.rb index 57f628f0..4162bd9a 100644 --- a/lib/openai/type/base_model.rb +++ b/lib/openai/type/base_model.rb @@ -331,6 +331,16 @@ def deconstruct_keys(keys) .to_h end + # @param a [Object] + # + # @return [String] + def to_json(*a) = self.class.dump(self).to_json(*a) + + # @param a [Object] + # + # @return [String] + def to_yaml(*a) = self.class.dump(self).to_yaml(*a) + # Create a new instance of a model. # # @param data [Hash{Symbol=>Object}, OpenAI::BaseModel] diff --git a/lib/openai/util.rb b/lib/openai/util.rb index 8a4b693f..379dddc4 100644 --- a/lib/openai/util.rb +++ b/lib/openai/util.rb @@ -441,7 +441,7 @@ class << self # # @yieldparam [Enumerator::Yielder] # @return [Enumerable] - def string_io(&blk) + def writable_enum(&blk) Enumerator.new do |y| y.define_singleton_method(:write) do self << _1.clone @@ -454,15 +454,13 @@ def string_io(&blk) end class << self - # rubocop:disable Naming/MethodParameterName - # @api private # # @param y [Enumerator::Yielder] # @param boundary [String] # @param key [Symbol, String] # @param val [Object] - private def encode_multipart_formdata(y, boundary:, key:, val:) + private def write_multipart_chunk(y, boundary:, key:, val:) y << "--#{boundary}\r\n" y << "Content-Disposition: form-data" unless key.nil? @@ -494,8 +492,6 @@ class << self y << "\r\n" end - # rubocop:enable Naming/MethodParameterName - # @api private # # @param body [Object] @@ -504,21 +500,21 @@ class << self private def encode_multipart_streaming(body) boundary = SecureRandom.urlsafe_base64(60) - strio = string_io do |y| + strio = writable_enum do |y| case body in Hash body.each do |key, val| case val in Array if val.all? { primitive?(_1) } val.each do |v| - encode_multipart_formdata(y, boundary: boundary, key: key, val: v) + write_multipart_chunk(y, boundary: boundary, key: key, val: v) end else - encode_multipart_formdata(y, boundary: boundary, key: key, val: val) + write_multipart_chunk(y, boundary: boundary, key: key, val: val) end end else - encode_multipart_formdata(y, boundary: boundary, key: nil, val: body) + write_multipart_chunk(y, boundary: boundary, key: nil, val: body) end y << "--#{boundary}--\r\n" end diff --git a/rbi/lib/openai/type/base_model.rbi b/rbi/lib/openai/type/base_model.rbi index e379903e..a35f37f5 100644 --- a/rbi/lib/openai/type/base_model.rbi +++ b/rbi/lib/openai/type/base_model.rbi @@ -176,6 +176,14 @@ module OpenAI def deconstruct_keys(keys) end + sig { params(a: T.anything).returns(String) } + def to_json(*a) + end + + sig { params(a: T.anything).returns(String) } + def to_yaml(*a) + end + # Create a new instance of a model. sig { params(data: T.any(T::Hash[Symbol, T.anything], T.self_type)).returns(T.attached_class) } def self.new(data = {}) diff --git a/rbi/lib/openai/util.rbi b/rbi/lib/openai/util.rbi index 79ce090a..57b25a19 100644 --- a/rbi/lib/openai/util.rbi +++ b/rbi/lib/openai/util.rbi @@ -198,7 +198,7 @@ module OpenAI class << self sig { params(blk: T.proc.params(y: Enumerator::Yielder).void).returns(T::Enumerable[String]) } - def string_io(&blk) + def writable_enum(&blk) end end @@ -207,7 +207,7 @@ module OpenAI sig do params(y: Enumerator::Yielder, boundary: String, key: T.any(Symbol, String), val: T.anything).void end - private def encode_multipart_formdata(y, boundary:, key:, val:) + private def write_multipart_chunk(y, boundary:, key:, val:) end # @api private diff --git a/sig/openai/type/base_model.rbs b/sig/openai/type/base_model.rbs index a929a421..cf3041f5 100644 --- a/sig/openai/type/base_model.rbs +++ b/sig/openai/type/base_model.rbs @@ -65,6 +65,10 @@ module OpenAI def deconstruct_keys: (::Array[Symbol]? keys) -> ::Hash[Symbol, top] + def to_json: (*top a) -> String + + def to_yaml: (*top a) -> String + def initialize: (?::Hash[Symbol, top] | self data) -> void def inspect: -> String diff --git a/sig/openai/util.rbs b/sig/openai/util.rbs index 97336cd4..1a93a427 100644 --- a/sig/openai/util.rbs +++ b/sig/openai/util.rbs @@ -82,11 +82,11 @@ module OpenAI } -> void end - def self?.string_io: { + def self?.writable_enum: { (Enumerator::Yielder y) -> void } -> Enumerable[String] - def self?.encode_multipart_formdata: ( + def self?.write_multipart_chunk: ( Enumerator::Yielder y, boundary: String, key: Symbol | String, diff --git a/test/openai/util_test.rb b/test/openai/util_test.rb index 476e16af..5d4c1b0c 100644 --- a/test/openai/util_test.rb +++ b/test/openai/util_test.rb @@ -233,7 +233,7 @@ def test_copy_write StringIO.new("abc") => "abc" } cases.each do |input, expected| - enum = OpenAI::Util.string_io do |y| + enum = OpenAI::Util.writable_enum do |y| IO.copy_stream(input, y) end assert_equal(expected, enum.to_a.join)