Skip to content

Commit

Permalink
Merge pull request #7 from shoplineapp/feature/support-json-string-fo…
Browse files Browse the repository at this point in the history
…r-hash

Feature/support json string for hash
  • Loading branch information
rayway30419 authored Feb 21, 2024
2 parents 6d359c1 + c17af84 commit be9cb1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ schema.to_avro.to_json
}
```
#### Hash: `string`
- Expected to be serialized as a JSON string.
```json
{
"type": "string",
"logicalType": "json"
}
```

### Embedded document
#### Embeds One
Expand Down
6 changes: 4 additions & 2 deletions lib/mongoid-avro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ def convert_to_avro_format(type: , optional: true)
default: []
}
when "Hash"
# Expect to encode unstructed data to json string
"string"
{
"type": "string",
"logicalType": "json"
}
else
# If the type is not recognized, raise an error
raise ArgumentError, "Unsupported type for avro_format: #{type}"
Expand Down
21 changes: 17 additions & 4 deletions spec/mongoid_avro_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,13 @@ def get_field_by_name(fields, name)
context "when default type is Hash" do
let(:field_name) { "object" }

it "convert to type: string" do
expect(field["type"]).to eq("string")
it "convert to type: string and logicalType: json" do
expect(field["type"]).to eq(
{
"logicalType" => "json",
"type" => "string"
}
)
end
end

Expand Down Expand Up @@ -260,8 +265,16 @@ def get_field_by_name(fields, name)
context "when default type is Hash" do
let(:field_name) { "object" }

it "convert to type: string" do
expect(field["type"]).to eq(["null", "string"])
it "convert to null or type: string with logicalType: json" do
expect(field["type"]).to eq(
[
"null",
{
"logicalType" => "json",
"type" => "string"
}
]
)
end
end

Expand Down

0 comments on commit be9cb1c

Please sign in to comment.