panko_serializer 0.8.3 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/database_matrix.yml +85 -0
- data/.github/workflows/docs.yml +1 -1
- data/.github/workflows/lint.yml +3 -3
- data/.github/workflows/tests.yml +34 -0
- data/Appraisals +15 -16
- data/Gemfile +1 -0
- data/README.md +4 -4
- data/docs/docs/associations.md +13 -13
- data/docs/docs/attributes.md +24 -17
- data/docs/docs/design-choices.md +17 -17
- data/docs/docs/getting-started.md +4 -4
- data/docs/docs/introduction.md +4 -4
- data/docs/docs/performance.md +4 -4
- data/docs/docs/response-bag.md +5 -5
- data/docs/docusaurus.config.js +2 -2
- data/docs/package-lock.json +5769 -3891
- data/docs/package.json +4 -7
- data/ext/panko_serializer/attributes_writer/active_record.c +2 -29
- data/ext/panko_serializer/attributes_writer/type_cast/type_cast.c +1 -1
- data/gemfiles/7.2.0.gemfile +3 -1
- data/gemfiles/7.2.0.gemfile.lock +101 -78
- data/gemfiles/8.0.0.gemfile +3 -1
- data/gemfiles/8.0.0.gemfile.lock +108 -88
- data/gemfiles/{7.0.0.gemfile → 8.1.0.gemfile} +6 -4
- data/gemfiles/8.1.0.gemfile.lock +248 -0
- data/lib/panko/version.rb +1 -1
- metadata +7 -12
- data/.github/workflows/ruby.yml +0 -51
- data/gemfiles/7.0.0.gemfile.lock +0 -176
- data/gemfiles/7.1.0.gemfile +0 -39
- data/gemfiles/7.1.0.gemfile.lock +0 -198
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7b4397a54b1f806131ee07ccaaa4062bba8eb5490c3810e5c6c3cc405b7167ec
|
|
4
|
+
data.tar.gz: 8f3127a38fe93844c3a82aa8e2a30ea82312ef1b0ebdf66f86bcd4761c392749
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 626bf9689a4be25a166a7ee3fe3b2274cfcc9b7bb17d57398d87301aca1c8039835d02ef37de647d62d3fa5c102cc22f752b2400e10240fa992d2007eaf9344c
|
|
7
|
+
data.tar.gz: 9a0048188abc83c9a064a0bb7721cc94a7f3965338f62a84fe4382a7b98f52941c1a1914f25dfee3bc7ed80168a1bb47f7def51954b9b1250029e4784f6f0a5d
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
name: Database Tests
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
database-matrix:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
|
|
9
|
+
strategy:
|
|
10
|
+
fail-fast: false
|
|
11
|
+
matrix:
|
|
12
|
+
ruby: ["3.4"]
|
|
13
|
+
rails: ["7.2.0", "8.0.0"]
|
|
14
|
+
database: ["sqlite", "postgresql", "mysql"]
|
|
15
|
+
|
|
16
|
+
services:
|
|
17
|
+
postgres:
|
|
18
|
+
image: postgres:17
|
|
19
|
+
env:
|
|
20
|
+
POSTGRES_DB: panko_test
|
|
21
|
+
POSTGRES_USER: postgres
|
|
22
|
+
POSTGRES_PASSWORD: password
|
|
23
|
+
options: >-
|
|
24
|
+
--health-cmd pg_isready
|
|
25
|
+
--health-interval 10s
|
|
26
|
+
--health-timeout 5s
|
|
27
|
+
--health-retries 5
|
|
28
|
+
ports:
|
|
29
|
+
- 5432:5432
|
|
30
|
+
|
|
31
|
+
mysql:
|
|
32
|
+
image: mysql:8.0
|
|
33
|
+
env:
|
|
34
|
+
MYSQL_DATABASE: panko_test
|
|
35
|
+
MYSQL_ROOT_PASSWORD: password
|
|
36
|
+
options: >-
|
|
37
|
+
--health-cmd="mysqladmin ping"
|
|
38
|
+
--health-interval=10s
|
|
39
|
+
--health-timeout=5s
|
|
40
|
+
--health-retries=3
|
|
41
|
+
ports:
|
|
42
|
+
- 3306:3306
|
|
43
|
+
|
|
44
|
+
steps:
|
|
45
|
+
- name: Install system dependencies
|
|
46
|
+
run: |
|
|
47
|
+
sudo apt update -y
|
|
48
|
+
sudo apt install -y libsqlite3-dev libpq-dev libmysqlclient-dev
|
|
49
|
+
|
|
50
|
+
- uses: actions/checkout@v5
|
|
51
|
+
|
|
52
|
+
- name: Configure MySQL authentication
|
|
53
|
+
if: matrix.database == 'mysql'
|
|
54
|
+
run: |
|
|
55
|
+
# Configure MySQL to use mysql_native_password for Trilogy compatibility
|
|
56
|
+
mysql -h 127.0.0.1 -P 3306 -u root -ppassword -e "
|
|
57
|
+
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
|
|
58
|
+
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
|
|
59
|
+
FLUSH PRIVILEGES;
|
|
60
|
+
"
|
|
61
|
+
|
|
62
|
+
- name: Set up Ruby ${{ matrix.ruby }}
|
|
63
|
+
uses: ruby/setup-ruby@v1
|
|
64
|
+
with:
|
|
65
|
+
ruby-version: ${{ matrix.ruby }}
|
|
66
|
+
bundler-cache: true
|
|
67
|
+
working-directory: .
|
|
68
|
+
env:
|
|
69
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile
|
|
70
|
+
DB: ${{ matrix.database }}
|
|
71
|
+
|
|
72
|
+
- name: Compile & test
|
|
73
|
+
env:
|
|
74
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile
|
|
75
|
+
DB: ${{ matrix.database }}
|
|
76
|
+
POSTGRES_HOST: localhost
|
|
77
|
+
POSTGRES_USER: postgres
|
|
78
|
+
POSTGRES_PASSWORD: password
|
|
79
|
+
POSTGRES_PORT: 5432
|
|
80
|
+
MYSQL_HOST: localhost
|
|
81
|
+
MYSQL_USER: root
|
|
82
|
+
MYSQL_PASSWORD: password
|
|
83
|
+
MYSQL_PORT: 3306
|
|
84
|
+
run: |
|
|
85
|
+
bundle exec rake
|
data/.github/workflows/docs.yml
CHANGED
data/.github/workflows/lint.yml
CHANGED
|
@@ -3,11 +3,11 @@ name: Lint
|
|
|
3
3
|
on: [push, pull_request]
|
|
4
4
|
|
|
5
5
|
jobs:
|
|
6
|
-
|
|
6
|
+
lint:
|
|
7
7
|
runs-on: ubuntu-latest
|
|
8
8
|
|
|
9
9
|
steps:
|
|
10
|
-
- uses: actions/checkout@
|
|
10
|
+
- uses: actions/checkout@v5
|
|
11
11
|
|
|
12
12
|
- name: Install deps
|
|
13
13
|
run: |
|
|
@@ -23,7 +23,7 @@ jobs:
|
|
|
23
23
|
bundle exec rake rubocop
|
|
24
24
|
|
|
25
25
|
- name: Lint C
|
|
26
|
-
uses: jidicula/clang-format-action@v4.
|
|
26
|
+
uses: jidicula/clang-format-action@v4.16.0
|
|
27
27
|
with:
|
|
28
28
|
clang-format-version: "16"
|
|
29
29
|
check-path: "ext/panko_serializer"
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Panko Serializer CI
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
tests:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
strategy:
|
|
9
|
+
fail-fast: false
|
|
10
|
+
matrix:
|
|
11
|
+
ruby: ["3.2", "3.3", "3.4"]
|
|
12
|
+
rails: ["7.2.0", "8.0.0", "8.1.0"]
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- name: Install deps
|
|
16
|
+
run: |
|
|
17
|
+
sudo apt update -y
|
|
18
|
+
sudo apt install -y libsqlite3-dev
|
|
19
|
+
|
|
20
|
+
- uses: actions/checkout@v5
|
|
21
|
+
- name: Set up Ruby ${{ matrix.ruby }}
|
|
22
|
+
uses: ruby/setup-ruby@v1
|
|
23
|
+
with:
|
|
24
|
+
ruby-version: ${{ matrix.ruby }}
|
|
25
|
+
bundler-cache: true
|
|
26
|
+
working-directory: .
|
|
27
|
+
env:
|
|
28
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile
|
|
29
|
+
|
|
30
|
+
- name: Compile & test
|
|
31
|
+
env:
|
|
32
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile
|
|
33
|
+
run: |
|
|
34
|
+
bundle exec rake
|
data/Appraisals
CHANGED
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
appraise "7.0.0" do
|
|
4
|
-
gem "sqlite3", "~> 1.4"
|
|
5
|
-
gem "activesupport", "~> 7.0.0"
|
|
6
|
-
gem "activemodel", "~> 7.0.0"
|
|
7
|
-
gem "activerecord", "~> 7.0.0", group: :test
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
appraise "7.1.0" do
|
|
11
|
-
gem "sqlite3", "~> 1.4"
|
|
12
|
-
gem "activesupport", "~> 7.1.5"
|
|
13
|
-
gem "activemodel", "~> 7.1.5"
|
|
14
|
-
gem "activerecord", "~> 7.1.5", group: :test
|
|
15
|
-
end
|
|
16
|
-
|
|
17
3
|
appraise "7.2.0" do
|
|
18
|
-
gem "sqlite3", "~> 1.4"
|
|
19
4
|
gem "activesupport", "~> 7.2.0"
|
|
20
5
|
gem "activemodel", "~> 7.2.0"
|
|
21
6
|
gem "activerecord", "~> 7.2.0", group: :test
|
|
7
|
+
|
|
8
|
+
gem "trilogy"
|
|
9
|
+
gem "sqlite3", "~> 1.4"
|
|
22
10
|
end
|
|
23
11
|
|
|
24
12
|
appraise "8.0.0" do
|
|
25
|
-
gem "sqlite3", ">= 2.1"
|
|
26
13
|
gem "activesupport", "~> 8.0.0"
|
|
27
14
|
gem "activemodel", "~> 8.0.0"
|
|
28
15
|
gem "activerecord", "~> 8.0.0", group: :test
|
|
16
|
+
|
|
17
|
+
gem "trilogy"
|
|
18
|
+
gem "sqlite3", ">= 2.1"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
appraise "8.1.0" do
|
|
22
|
+
gem "activesupport", "~> 8.1.0"
|
|
23
|
+
gem "activemodel", "~> 8.1.0"
|
|
24
|
+
gem "activerecord", "~> 8.1.0", group: :test
|
|
25
|
+
|
|
26
|
+
gem "trilogy"
|
|
27
|
+
gem "sqlite3", ">= 2.1"
|
|
29
28
|
end
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -4,20 +4,20 @@
|
|
|
4
4
|
|
|
5
5
|
Panko is a library which is inspired by ActiveModelSerializers 0.9 for serializing ActiveRecord/Ruby objects to JSON strings, fast.
|
|
6
6
|
|
|
7
|
-
To achieve its [performance](https://panko.dev/
|
|
7
|
+
To achieve its [performance](https://panko.dev/performance/):
|
|
8
8
|
|
|
9
9
|
* Oj - Panko relies on Oj since it's fast and allows for incremental serialization using `Oj::StringWriter`
|
|
10
10
|
* Serialization Descriptor - Panko computes most of the metadata ahead of time, to save time later in serialization.
|
|
11
11
|
* Type casting — Panko does type casting by itself, instead of relying on ActiveRecord.
|
|
12
12
|
|
|
13
|
-
To dig deeper about the performance choices, read [Design Choices](https://panko.dev/
|
|
13
|
+
To dig deeper about the performance choices, read [Design Choices](https://panko.dev/design-choices/).
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
Support
|
|
17
17
|
-------
|
|
18
18
|
|
|
19
|
-
- [Documentation](https://panko.dev/
|
|
20
|
-
- [Getting Started](https://panko.dev/
|
|
19
|
+
- [Documentation](https://panko.dev/)
|
|
20
|
+
- [Getting Started](https://panko.dev/getting-started/)
|
|
21
21
|
|
|
22
22
|
License
|
|
23
23
|
-------
|
data/docs/docs/associations.md
CHANGED
|
@@ -3,7 +3,7 @@ id: associations
|
|
|
3
3
|
title: Associations
|
|
4
4
|
sidebar_label: Associations
|
|
5
5
|
---
|
|
6
|
-
A serializer can define it's own associations - both `has_many` and `has_one` to
|
|
6
|
+
A serializer can define it's own associations - both `has_many` and `has_one` to serialize under the context of the object.
|
|
7
7
|
|
|
8
8
|
For example:
|
|
9
9
|
|
|
@@ -38,8 +38,8 @@ end
|
|
|
38
38
|
|
|
39
39
|
### Inference
|
|
40
40
|
|
|
41
|
-
Panko can find the type of the serializer by looking at the
|
|
42
|
-
the serializer at the above example, we can
|
|
41
|
+
Panko can find the type of the serializer by looking at the relationship name, so instead of specifying
|
|
42
|
+
the serializer at the above example, we can:
|
|
43
43
|
|
|
44
44
|
```ruby
|
|
45
45
|
|
|
@@ -52,19 +52,19 @@ end
|
|
|
52
52
|
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
The logic of inferencing is
|
|
55
|
+
The logic of inferencing is:
|
|
56
56
|
|
|
57
|
-
- Take the name of the relationship (for example - `:author` / `:comments`) singularize and camelize it
|
|
58
|
-
- Look for const defined with the name
|
|
57
|
+
- Take the name of the relationship (for example - `:author` / `:comments`) singularize and camelize it.
|
|
58
|
+
- Look for const defined with the name above and "Serializer" suffix (by using `Object.const_get`).
|
|
59
59
|
|
|
60
|
-
> If Panko can't find the serializer it will throw an error on startup time, for example: `Can't find serializer for PostSerializer.author has_one relationship
|
|
60
|
+
> If Panko can't find the serializer it will throw an error on startup time, for example: `Can't find serializer for PostSerializer.author has_one relationship`.
|
|
61
61
|
|
|
62
62
|
## Nested Filters
|
|
63
63
|
|
|
64
64
|
As talked before, Panko allows you to filter the attributes of a serializer.
|
|
65
|
-
But Panko
|
|
65
|
+
But Panko lets you take that step further, and filters the attributes of you associations so you can re-use your serializers in your application.
|
|
66
66
|
|
|
67
|
-
For example, let's say one portion of the application needs to
|
|
67
|
+
For example, let's say one portion of the application needs to serialize a list of posts but only with their - `title`, `body`, author's id and comments id.
|
|
68
68
|
|
|
69
69
|
We can declare tailored serializer for this, or we can re-use the above defined serializer - `PostSerializer` and use nested filters.
|
|
70
70
|
|
|
@@ -80,14 +80,14 @@ Panko::ArraySerializer.new(posts, each_serializer: PostSerializer, only: {
|
|
|
80
80
|
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
Let's dissect `only` option we passed
|
|
83
|
+
Let's dissect the `only` option we passed:
|
|
84
84
|
|
|
85
|
-
- `instance` - list of attributes (and associations) we want to
|
|
85
|
+
- `instance` - list of attributes (and associations) we want to serialize for the current instance of the serializer, in this case - `PostSerializer`.
|
|
86
86
|
- `author`, `comments` - here we specify the list of attributes we want to serialize for each association.
|
|
87
87
|
|
|
88
|
-
It's important to note that Nested Filters
|
|
88
|
+
It's important to note that Nested Filters are recursive, in other words, we can filter the association's associations.
|
|
89
89
|
|
|
90
|
-
For example, `CommentSerializer`
|
|
90
|
+
For example, `CommentSerializer` has an `has_one` association `Author`, and for each `comments.author` we can only serialize it's name.
|
|
91
91
|
|
|
92
92
|
```ruby
|
|
93
93
|
|
data/docs/docs/attributes.md
CHANGED
|
@@ -3,7 +3,8 @@ id: attributes
|
|
|
3
3
|
title: Attributes
|
|
4
4
|
sidebar_label: Attributes
|
|
5
5
|
---
|
|
6
|
-
Attributes allow you to specify which record attributes you want to serialize
|
|
6
|
+
Attributes allow you to specify which record attributes you want to serialize.
|
|
7
|
+
|
|
7
8
|
There are two types of attributes:
|
|
8
9
|
|
|
9
10
|
- Field - simple columns defined on the record it self.
|
|
@@ -25,13 +26,13 @@ end
|
|
|
25
26
|
|
|
26
27
|
Using field attributes you can control which columns of the given ActiveRecord object you want to serialize.
|
|
27
28
|
|
|
28
|
-
Instead of relying ActiveRecord to do it's type casting, Panko does on it's own for performance reasons (read more in [Design Choices](design-choices.md#type-casting)).
|
|
29
|
+
Instead of relying on ActiveRecord to do it's type casting, Panko does on it's own for performance reasons (read more in [Design Choices](design-choices.md#type-casting)).
|
|
29
30
|
|
|
30
31
|
## Method Attributes
|
|
31
32
|
|
|
32
33
|
Method attributes are used when your serialized values can be derived from the object you are serializing.
|
|
33
34
|
|
|
34
|
-
The serializer's attribute methods can access the object being serialized as `object
|
|
35
|
+
The serializer's attribute methods can access the object being serialized as `object`:
|
|
35
36
|
|
|
36
37
|
```ruby
|
|
37
38
|
|
|
@@ -45,7 +46,7 @@ end
|
|
|
45
46
|
|
|
46
47
|
```
|
|
47
48
|
|
|
48
|
-
Another useful
|
|
49
|
+
Another useful thing you can pass your serializer is `context`, a `context` is a bag of data whom your serializer may need.
|
|
49
50
|
|
|
50
51
|
For example, here we will pass feature flags:
|
|
51
52
|
|
|
@@ -73,8 +74,8 @@ Filters allows us to reduce the amount of attributes we can serialize, therefore
|
|
|
73
74
|
|
|
74
75
|
There are two types of filters:
|
|
75
76
|
|
|
76
|
-
- only - use those attributes **only** and nothing else
|
|
77
|
-
- except - all attributes **except** those attributes
|
|
77
|
+
- only - use those attributes **only** and nothing else.
|
|
78
|
+
- except - all attributes **except** those attributes.
|
|
78
79
|
|
|
79
80
|
Usage example:
|
|
80
81
|
|
|
@@ -92,15 +93,13 @@ UserSerializer.new(except: [:name]).serialize(User.first)
|
|
|
92
93
|
|
|
93
94
|
```
|
|
94
95
|
|
|
95
|
-
> **Note** that if you want to user filter on an associations, the `:name`
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
> defined, the key to use in filters is `:state_transitions`
|
|
99
|
-
> (e.g. `{ except: [:state_transitions] }`)
|
|
96
|
+
> **Note** that if you want to user filter on an associations, the `:name` property is not taken into account.
|
|
97
|
+
If you have a `has_many :state_transitions, name: :history` association defined, the key to use in filters is
|
|
98
|
+
`:state_transitions` (e.g. `{ except: [:state_transitions] }`).
|
|
100
99
|
|
|
101
100
|
## Filters For
|
|
102
101
|
|
|
103
|
-
Sometimes you find yourself
|
|
102
|
+
Sometimes you find yourself having the same filtering logic in actions. In order to
|
|
104
103
|
solve this duplication, Panko allows you to write the filters in the serializer.
|
|
105
104
|
|
|
106
105
|
```ruby
|
|
@@ -120,24 +119,32 @@ UserSerializer.serialize(User.first)
|
|
|
120
119
|
|
|
121
120
|
```
|
|
122
121
|
|
|
123
|
-
> See discussion in: https:
|
|
122
|
+
> See discussion in: [https:](https://github.com/yosiat/panko_serializer/issues/16)
|
|
124
123
|
|
|
125
124
|
## Aliases
|
|
126
125
|
|
|
127
|
-
Let's say we have attribute name that we want to expose to client as different name, the current way of doing so is using method attribute, for example:
|
|
126
|
+
Let's say we have an attribute name that we want to expose to client as different name, the current way of doing so is using method attribute, for example:
|
|
128
127
|
|
|
129
128
|
```ruby
|
|
130
129
|
|
|
130
|
+
class PostSerializer < Panko::Serializer
|
|
131
|
+
attributes :published_at
|
|
131
132
|
|
|
133
|
+
def published_at
|
|
134
|
+
object.created_at
|
|
135
|
+
end
|
|
136
|
+
end
|
|
132
137
|
|
|
133
138
|
```
|
|
134
139
|
|
|
135
|
-
The downside of this approach is that
|
|
140
|
+
The downside of this approach is that `created_at` skips Panko's type casting, therefore we get a direct hit on performance.
|
|
136
141
|
|
|
137
|
-
To fix this, we can use aliases
|
|
142
|
+
To fix this, we can use aliases:
|
|
138
143
|
|
|
139
144
|
```ruby
|
|
140
145
|
|
|
141
|
-
|
|
146
|
+
class PostSerializer < Panko::Serializer
|
|
147
|
+
aliases created_at: :published_at
|
|
148
|
+
end
|
|
142
149
|
|
|
143
150
|
```
|
data/docs/docs/design-choices.md
CHANGED
|
@@ -3,7 +3,7 @@ id: design-choices
|
|
|
3
3
|
title: Design Choices
|
|
4
4
|
sidebar_label: Design Choices
|
|
5
5
|
---
|
|
6
|
-
In short, Panko
|
|
6
|
+
In short, Panko is a serializer for ActiveRecord objects (it can't serialize any other object), which strives for high performance & simple API (which is inspired by ActiveModelSerializers).
|
|
7
7
|
|
|
8
8
|
Its performance is achieved by:
|
|
9
9
|
|
|
@@ -13,7 +13,7 @@ Its performance is achieved by:
|
|
|
13
13
|
|
|
14
14
|
## Serialization overview
|
|
15
15
|
|
|
16
|
-
First, let's start with overview. Let's say we want to serialize `User` object, which has
|
|
16
|
+
First, let's start with an overview. Let's say we want to serialize an `User` object, which has
|
|
17
17
|
`first_name`, `last_name`, `age`, and `email` properties.
|
|
18
18
|
|
|
19
19
|
The serializer definition will be something like this:
|
|
@@ -46,14 +46,14 @@ serializer.serialize_to_json(user)
|
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
Let's go over the steps that Panko will execute behind the scenes for this flow.
|
|
49
|
-
_I will skip the serializer definition part, because it's fairly simple and straightforward (see `lib/panko/serializer.rb`)_
|
|
49
|
+
_I will skip the serializer definition part, because it's fairly simple and straightforward (see `lib/panko/serializer.rb`)._
|
|
50
50
|
|
|
51
51
|
First step, while initializing the UserSerializer, we will create a **Serialization Descriptor** for this class.
|
|
52
52
|
Serialization Descriptor's goal is to answer those questions:
|
|
53
53
|
|
|
54
|
-
- Which fields do we have? In our case, `:age`, `:email
|
|
55
|
-
- Which method fields do we have? In our case `:name
|
|
56
|
-
- Which associations do we have (and their serialization descriptors)
|
|
54
|
+
- Which fields do we have? In our case, `:age`, `:email`.
|
|
55
|
+
- Which method fields do we have? In our case `:name`.
|
|
56
|
+
- Which associations do we have (and their serialization descriptors)?
|
|
57
57
|
|
|
58
58
|
The serialization description is also responsible for filtering the attributes (`only` \\ `except`).
|
|
59
59
|
|
|
@@ -65,7 +65,7 @@ In C land, we take the `user` object and the serialization descriptor, and start
|
|
|
65
65
|
- Serializing Method Fields - creating (a cached) serializer instance, setting its `@object` and `@context`, calling all the method fields and writing them to the writer.
|
|
66
66
|
- Serializing associations — this is simple, once we have fields + method fields, we just repeat the process.
|
|
67
67
|
|
|
68
|
-
Once this is finished, we have nice JSON string.
|
|
68
|
+
Once this is finished, we have a nice JSON string.
|
|
69
69
|
Now let's dig deeper.
|
|
70
70
|
|
|
71
71
|
## Interesting parts
|
|
@@ -74,36 +74,36 @@ Now let's dig deeper.
|
|
|
74
74
|
|
|
75
75
|
If you read the code of ActiveRecord serialization code in Ruby, you will observe this flow:
|
|
76
76
|
|
|
77
|
-
1. Get an array of ActiveRecord objects (`User.all` for example)
|
|
78
|
-
2. Build new array of hashes where each hash is `User` with the attributes we selected
|
|
79
|
-
3. The JSON serializer, takes this array of hashes and loop them, and converts it to JSON string
|
|
77
|
+
1. Get an array of ActiveRecord objects (`User.all` for example).
|
|
78
|
+
2. Build a new array of hashes where each hash is an `User` with the attributes we selected.
|
|
79
|
+
3. The JSON serializer, takes this array of hashes and loop them, and converts it to a JSON string.
|
|
80
80
|
|
|
81
81
|
This entire process is expensive in terms of Memory & CPU, and this where the combination of Panko and Oj::StringWriter really shines.
|
|
82
82
|
|
|
83
83
|
In Panko, the serialization process of the above is:
|
|
84
84
|
|
|
85
|
-
1. Get an array of ActiveRecord objects (`User.all` for example)
|
|
85
|
+
1. Get an array of ActiveRecord objects (`User.all` for example).
|
|
86
86
|
2. Create `Oj::StringWriter` and feed the values to it, via `push_value` / `push_object` / `push_object` and behind the scene, `Oj::StringWriter` will serialize the objects incrementally into a string.
|
|
87
87
|
3. Get from `Oj::StringWriter` the completed JSON string — which is a no-op, since `Oj::StringWriter` already built the string.
|
|
88
88
|
|
|
89
89
|
### Figuring out the metadata, ahead of time.
|
|
90
90
|
|
|
91
|
-
Another observation I noticed in the
|
|
91
|
+
Another observation I noticed in the Ruby serializers is that they ask and do a lot in a serialization loop:
|
|
92
92
|
|
|
93
93
|
- Is this field a method? is it a property?
|
|
94
|
-
- Which fields and associations do I need for the serializer to consider the `only` and `except` options
|
|
94
|
+
- Which fields and associations do I need for the serializer to consider the `only` and `except` options?
|
|
95
95
|
- What is the serializer of this has_one association?
|
|
96
96
|
|
|
97
97
|
Panko tries to ask the bare minimum in serialization by building `Serialization Descriptor` for each serialization and caching it.
|
|
98
98
|
|
|
99
|
-
The Serialization Descriptor will do the filtering of `only` and `except` and will check if a field is a method or not (therefore Panko doesn't have list of `attributes`)
|
|
99
|
+
The Serialization Descriptor will do the filtering of `only` and `except` and will check if a field is a method or not (therefore Panko doesn't have list of `attributes`).
|
|
100
100
|
|
|
101
101
|
### Type Casting
|
|
102
102
|
|
|
103
103
|
This is the final part, which helped yield most of the performance improvements.
|
|
104
|
-
In ActiveRecord, when we read
|
|
104
|
+
In ActiveRecord, when we read the value of an attribute, it does type casting of the DB value to its real Ruby type.
|
|
105
105
|
|
|
106
|
-
For example, time strings are converted to Time objects, Strings are duplicated, and Integers are
|
|
106
|
+
For example, time strings are converted to Time objects, Strings are duplicated, and Integers are converted from their values to Number.
|
|
107
107
|
|
|
108
108
|
This type casting is really expensive, as it's responsible for most of the allocations in the serialization flow and most of them can be "relaxed".
|
|
109
109
|
|
|
@@ -123,5 +123,5 @@ The idea behind the time type casting code relies on the end result of JSON type
|
|
|
123
123
|
The time type casting works as follows:
|
|
124
124
|
|
|
125
125
|
- If it's a string that ends with `Z`, and the strings matches the UTC ISO8601 regex, then we just return the string.
|
|
126
|
-
- If it's a string and it doesn't follow the rules above, we check if it's a timestamp in database format and convert it via regex + string concat to UTC ISO8601 - Yes, there is huge assumption here, that the database returns UTC timestamps — this will be
|
|
126
|
+
- If it's a string and it doesn't follow the rules above, we check if it's a timestamp in database format and convert it via regex + string concat to UTC ISO8601 - Yes, there is huge assumption here, that the database returns UTC timestamps — this will be configurable (before Panko official release).
|
|
127
127
|
- If it's none of the above, I will let ActiveRecord type casting do it's magic.
|
|
@@ -23,7 +23,7 @@ Then, install it on the command line:
|
|
|
23
23
|
|
|
24
24
|
## Creating your first serializer
|
|
25
25
|
|
|
26
|
-
Let's create serializer and use it inside Rails controller
|
|
26
|
+
Let's create a serializer and use it inside of a Rails controller:
|
|
27
27
|
|
|
28
28
|
```ruby
|
|
29
29
|
class PostSerializer < Panko::Serializer
|
|
@@ -39,7 +39,7 @@ end
|
|
|
39
39
|
|
|
40
40
|
### Serializing an object
|
|
41
41
|
|
|
42
|
-
And now serialize a single object
|
|
42
|
+
And now serialize a single object:
|
|
43
43
|
|
|
44
44
|
```ruby
|
|
45
45
|
|
|
@@ -53,7 +53,7 @@ PostSerializer.new.serialize(Post.first).to_json
|
|
|
53
53
|
|
|
54
54
|
### Using the serializers in a controller
|
|
55
55
|
|
|
56
|
-
As you can see, defining serializers is simple and resembles ActiveModelSerializers 0.9
|
|
56
|
+
As you can see, defining serializers is simple and resembles ActiveModelSerializers 0.9.
|
|
57
57
|
To utilize the `UserSerializer` inside a Rails controller and serialize some users, all we need to do is:
|
|
58
58
|
|
|
59
59
|
```ruby
|
|
@@ -67,4 +67,4 @@ end
|
|
|
67
67
|
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
And voila, we have endpoint which
|
|
70
|
+
And voila, we have an endpoint which serializes users using Panko!
|
data/docs/docs/introduction.md
CHANGED
|
@@ -4,10 +4,10 @@ title: Introduction
|
|
|
4
4
|
sidebar_label: Introduction
|
|
5
5
|
slug: /
|
|
6
6
|
---
|
|
7
|
-
Panko is library which is inspired by ActiveModelSerializers 0.9 for serializing ActiveRecord/Ruby objects to JSON strings, fast.
|
|
7
|
+
Panko is a library which is inspired by ActiveModelSerializers 0.9 for serializing ActiveRecord/Ruby objects to JSON strings, fast.
|
|
8
8
|
|
|
9
|
-
To achieve it's [performance](https://panko.dev/
|
|
9
|
+
To achieve it's [performance](https://panko.dev/performance/):
|
|
10
10
|
|
|
11
|
-
- Oj - Panko relies Oj since it's fast and allow to serialize incrementally using `Oj::StringWriter
|
|
11
|
+
- Oj - Panko relies on Oj since it's fast and allow to serialize incrementally using `Oj::StringWriter`.
|
|
12
12
|
- Serialization Descriptor - Panko computes most of the metadata ahead of time, to save time later in serialization.
|
|
13
|
-
- Type casting — Panko does type casting by
|
|
13
|
+
- Type casting — Panko does type casting by itself, instead of relying on ActiveRecord.
|
data/docs/docs/performance.md
CHANGED
|
@@ -8,7 +8,7 @@ The performance of Panko is measured using microbenchmarks and load testing.
|
|
|
8
8
|
## Microbenchmarks
|
|
9
9
|
|
|
10
10
|
The following microbenchmarks are run on MacBook Pro (16-inch, 2021, M1 Max), Ruby 3.2.0 with Rails 7.0.5
|
|
11
|
-
demonstrating the performance of ActiveModelSerializers 0.10.13 and Panko 0.8.0
|
|
11
|
+
demonstrating the performance of ActiveModelSerializers 0.10.13 and Panko 0.8.0.
|
|
12
12
|
|
|
13
13
|
| Benchmark | AMS ip/s | Panko ip/s |
|
|
14
14
|
| ----------------- | -------- | ---------- |
|
|
@@ -19,8 +19,8 @@ demonstrating the performance of ActiveModelSerializers 0.10.13 and Panko 0.8.0
|
|
|
19
19
|
|
|
20
20
|
## Real-world benchmark
|
|
21
21
|
|
|
22
|
-
The real-world benchmark here is endpoint which serializes 7,884 entries with 48 attributes and no associations.
|
|
23
|
-
The benchmark took place in environment that simulates production environment and run using `wrk` from machine on the same cluster.
|
|
22
|
+
The real-world benchmark here is an endpoint which serializes 7,884 entries with 48 attributes and no associations.
|
|
23
|
+
The benchmark took place in an environment that simulates production environment and run using `wrk` from machine on the same cluster.
|
|
24
24
|
|
|
25
25
|
| Metric | AMS | Panko |
|
|
26
26
|
| ------------------ | ----- | ----- |
|
|
@@ -29,4 +29,4 @@ The benchmark took place in environment that simulates production environment an
|
|
|
29
29
|
| 99th Response Time | 5.42s | 1.74s |
|
|
30
30
|
| Total Requests | 61 | 202 |
|
|
31
31
|
|
|
32
|
-
_Thanks to [Bringg](https://www.bringg.com) for providing the infrastructure for the
|
|
32
|
+
_Thanks to [Bringg](https://www.bringg.com) for providing the infrastructure for the benchmarks._
|
data/docs/docs/response-bag.md
CHANGED
|
@@ -3,7 +3,7 @@ id: response-bag
|
|
|
3
3
|
title: Response
|
|
4
4
|
sidebar_label: Response
|
|
5
5
|
---
|
|
6
|
-
Let's say you have some JSON payload which
|
|
6
|
+
Let's say you have some JSON payload which is constructed using Panko serialization result,
|
|
7
7
|
like this:
|
|
8
8
|
|
|
9
9
|
```ruby
|
|
@@ -21,7 +21,7 @@ end
|
|
|
21
21
|
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
The output of the above will be
|
|
24
|
+
The output of the above will be a JSON string (for `posts`) inside a JSON string and this were `Panko::Response` shines.
|
|
25
25
|
|
|
26
26
|
```ruby
|
|
27
27
|
|
|
@@ -40,7 +40,7 @@ end
|
|
|
40
40
|
|
|
41
41
|
And everything will work as expected!
|
|
42
42
|
|
|
43
|
-
For a single object serialization, we need to use a different API (since `Panko::Serializer`
|
|
43
|
+
For a single object serialization, we need to use a different API (since `Panko::Serializer` doesn't accept an object in it's constructor):
|
|
44
44
|
|
|
45
45
|
```ruby
|
|
46
46
|
|
|
@@ -63,8 +63,8 @@ end
|
|
|
63
63
|
|
|
64
64
|
## JsonValue
|
|
65
65
|
|
|
66
|
-
Let's take the above example further, we
|
|
67
|
-
Now, you can wrap the cached value with `Panko::JsonValue`, like here
|
|
66
|
+
Let's take the above example further, we will serialize the posts and cache it as JSON string in our Cache.
|
|
67
|
+
Now, you can wrap the cached value with `Panko::JsonValue`, like here:
|
|
68
68
|
|
|
69
69
|
```ruby
|
|
70
70
|
|
data/docs/docusaurus.config.js
CHANGED
|
@@ -78,8 +78,8 @@ module.exports = {
|
|
|
78
78
|
"copyright": `Copyright © ${new Date().getFullYear()} Panko Serializer`,
|
|
79
79
|
},
|
|
80
80
|
prism: {
|
|
81
|
-
theme: require('prism-react-renderer
|
|
82
|
-
darkTheme: require('prism-react-renderer
|
|
81
|
+
theme: require('prism-react-renderer').themes.github, // Optional: Customize theme
|
|
82
|
+
darkTheme: require('prism-react-renderer').themes.dracula, // Optional: Dark theme
|
|
83
83
|
additionalLanguages: ['ruby'], // Add Ruby as an additional language
|
|
84
84
|
},
|
|
85
85
|
}
|