Skip to content

Commit

Permalink
Retrieve comments from database tables, override, and output
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Nov 14, 2024
1 parent 497500f commit 62db116
Show file tree
Hide file tree
Showing 33 changed files with 55 additions and 28 deletions.
2 changes: 2 additions & 0 deletions lib/typelizer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config < Struct.new(
:types_import_path,
:types_global,
:verbatim_module_syntax,
:comments,
keyword_init: true
) do
class << self
Expand All @@ -46,6 +47,7 @@ def instance

type_mapping: TYPE_MAPPING,
null_strategy: :nullable,
comments: false,

output_dir: js_root.join("types/serializers"),

Expand Down
6 changes: 5 additions & 1 deletion lib/typelizer/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def infer_types(props, hash_name = :_typelizer_attributes)
props.map do |prop|
if serializer.respond_to?(hash_name)
dsl_type = serializer.public_send(hash_name)[prop.name.to_sym]
next Property.new(prop.to_h.merge(dsl_type)) if dsl_type&.any?
if dsl_type&.any?
next Property.new(prop.to_h.merge(dsl_type)).tap do |property|
property.comment ||= model_plugin.comment_for(property) if config.comments && property.comment != false
end
end
end

model_plugin.infer_types(prop)
Expand Down
8 changes: 8 additions & 0 deletions lib/typelizer/model_plugins/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ def infer_types(prop)
end

prop.type = @config.type_mapping[column.type]
prop.comment = comment_for(prop)

prop
end

def comment_for(prop)
column = columns_hash[prop.column_name.to_s]
return nil unless column

prop.comment = column.comment
end
end
end
end
4 changes: 4 additions & 0 deletions lib/typelizer/model_plugins/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def initialize(...)
def infer_types(prop)
prop
end

def comment_for(prop)
nil
end
end
end
end
2 changes: 1 addition & 1 deletion lib/typelizer/property.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Typelizer
Property = Struct.new(
:name, :type, :optional, :nullable,
:multi, :column_name,
:multi, :column_name, :comment,
keyword_init: true
) do
def inspect
Expand Down
1 change: 1 addition & 0 deletions lib/typelizer/templates/interface.ts.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type <%= interface.name %> = {
<%- else -%>
type <%= interface.name %> = {
<%- interface.properties.each do |property| -%>
<%= indent("// #{property.comment.tr("\n", '\n')}\n") if interface.config.comments && property.comment -%>
<%= indent(property) %>;
<%- end -%>
}
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaInline.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 31c711d30b829018bdc69e3e09ca8e72
// Typelizer digest f5275d68218cedacb480ce36d1e6bbe2
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaMeta.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 324c9ad635869485fb8231dfbed8cffe
// Typelizer digest bf4b0c896fc166774c2208b110c6d2e9
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaMetaNil.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 22be4653e9abf0c8b5e2963602fbf179
// Typelizer digest aa6e6401d09df2e398c69ccd17c99ee5
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaPoro.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 3c3acd1390157d9666f0f2be46bfcb1c
// Typelizer digest 4a7987ac34502a3281b1f42e3f266099
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaPost.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 37cf53e5c2445bf410d20f0dc76526d9
// Typelizer digest 3ab2e20940a1c4559f44fb582ad7719d
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaUser} from '@/types'
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaUser.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 9639758a07e00e4992619fbdef2450db
// Typelizer digest 480b9d1caeafc717431d31282ba0981c
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaPost} from '@/types'
Expand Down
3 changes: 2 additions & 1 deletion spec/__snapshots__/AlbaUserAuthor.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Typelizer digest cea56a3dcb747f59b3998779acbe93f8
// Typelizer digest 5853f063c6c19b98aeeee99e9c29980a
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaPost} from '@/types'

type AlbaUserAuthor = {
id: number;
// Author login handle
username: string | null;
posts?: Array<AlbaPost>;
avatar: unknown;
Expand Down
3 changes: 2 additions & 1 deletion spec/__snapshots__/AlbaUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Typelizer digest 1002a651beda81fd3d91636e80542552
// Typelizer digest 3bf7801f630edffea7eefdcb86e3fb74
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaPost} from '@/types'

type AlbaUserEmptyNested = {
id: number;
// Author login handle
username: string | null;
posts?: Array<AlbaPost>;
avatar: unknown;
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaUserSerializerFoo.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest c6fb5013c54f52cedcbedeaef1455ff1
// Typelizer digest 4e7d58f3d78c500545098dad7137b8c6
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AlbaUser, AlbaPost} from '@/types'
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest a6d580654a084cc58071460e42093126
// Typelizer digest f82208b39a056eaa4fdfc6b8696f7c93
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AmsPost.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 38cf3a9940c0128748ccff969ac2d9ac
// Typelizer digest 49f4596d7eb72effcbb13a7b5a05ea44
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AmsUser} from '@/types'
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AmsUser.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 83c10371506a5252b3c5eb44ff747d45
// Typelizer digest 1aea91828e4e0f1dfdd795922b602e40
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AmsPost} from '@/types'
Expand Down
3 changes: 2 additions & 1 deletion spec/__snapshots__/AmsUserAuthor.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Typelizer digest d00424b465021050da1b0cca9388df55
// Typelizer digest 6b3157b1afb8bd20ec259745483e6069
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AmsPost} from '@/types'

type AmsUserAuthor = {
id: number;
// Author login handle
username: string | null;
avatar: unknown;
typed_avatar: string | null;
Expand Down
3 changes: 2 additions & 1 deletion spec/__snapshots__/AmsUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Typelizer digest 8c962041f6d8142284309a7a7695ccff
// Typelizer digest fe35a8cc2cd60cd90112d82d3886a6ad
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AmsPost} from '@/types'

type AmsUserEmptyNested = {
id: number;
// Author login handle
username: string | null;
avatar: unknown;
typed_avatar: string | null;
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AmsUserSerializerFoo.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest c3e52d2a5d6865f2ee8a01c8b8d38dd2
// Typelizer digest 01c1c8d2bbca94c0ac46a9dedfc1d312
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {AmsUser, AmsPost} from '@/types'
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 4f6b6fed16d511dbfe9a02a0b7aa9c41
// Typelizer digest 7c09585f88197784fbfde340bfd5e36b
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/OjSerializersFlatUser.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest d0613bfadf56720cfde6062373be13a6
// Typelizer digest 5fc05a8d823c221cd138c49d17ccd54c
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OjSerializersUser, OjSerializersPost} from '@/types'
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/OjSerializersPost.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest e7ea4668b82a2d60552e8eaf7ada3d7a
// Typelizer digest b05ae512914de90ee4c78f8b8d5ef96a
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OjSerializersUser} from '@/types'
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/OjSerializersUser.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 63fa5a28b5147a978506b2c0e5f3f398
// Typelizer digest fc111d0651504bd12951008cbc0bfffd
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OjSerializersPost} from '@/types'
Expand Down
3 changes: 2 additions & 1 deletion spec/__snapshots__/OjSerializersUserAuthor.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Typelizer digest 6c423801303540bc8b86331341f9a08c
// Typelizer digest 8b7f37c2d5f826ce3dfb592d5774a1a5
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OjSerializersPost} from '@/types'

type OjSerializersUserAuthor = {
id: number;
// Author login handle
username: string | null;
posts?: Array<OjSerializersPost>;
avatar: unknown;
Expand Down
3 changes: 2 additions & 1 deletion spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Typelizer digest 792747cc9a6687f61568633186ee265d
// Typelizer digest 2ebe03be885472fb7f96144781d418f1
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OjSerializersPost} from '@/types'

type OjSerializersUserEmptyNested = {
id: number;
// Author login handle
username: string | null;
posts?: Array<OjSerializersPost>;
avatar: unknown;
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/OjSerializersUserSerializerFoo.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest 4b93ca3b7d3283f9147217b575696867
// Typelizer digest 220ec4296faaf24a55e668b3ff1cbccd
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {OjSerializersUser, OjSerializersPost} from '@/types'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Typelizer digest c0f49e6c7ece40d4d23624555e33da67
// Typelizer digest e4b0b8226d1bf89d94bd35bc366e5837
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.

Expand Down
2 changes: 1 addition & 1 deletion spec/app/app/serializers/alba/user/author_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module User
class AuthorSerializer < BaseSerializer
typelize_from ::User

typelize username: [:string, nullable: true]
typelize username: [:string, nullable: true, comment: "Author login handle"]
attributes :id, :username

has_many :posts, resource: PostSerializer, if: ->(u) { u.posts.any? }
Expand Down
2 changes: 1 addition & 1 deletion spec/app/app/serializers/ams/user/author_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module User
class AuthorSerializer < BaseSerializer
typelize_from ::User

typelize username: [:string, nullable: true]
typelize username: [:string, nullable: true, comment: "Author login handle"]
attributes :id, :username

has_many :posts, serializer: PostSerializer, if: ->(u) { u.posts.any? }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module User
class AuthorSerializer < BaseSerializer
typelize_from ::User

typelize username: [:string, nullable: true]
typelize username: [:string, nullable: true, comment: "Author login handle"]
attributes :id, :username

has_many :posts, serializer: PostSerializer, if: ->(u) { u.posts.any? }
Expand Down
2 changes: 2 additions & 0 deletions spec/app/config/initializers/typelizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
]

c.types_global = %w[Array Date Record]

c.comments = true
end

0 comments on commit 62db116

Please sign in to comment.