From 62db116c1e4749943e55b113931ee7c4f0dc4ad6 Mon Sep 17 00:00:00 2001 From: Andrey Novikov Date: Thu, 14 Nov 2024 20:00:18 +0900 Subject: [PATCH] Retrieve comments from database tables, override, and output --- lib/typelizer/config.rb | 2 ++ lib/typelizer/interface.rb | 6 +++++- lib/typelizer/model_plugins/active_record.rb | 8 ++++++++ lib/typelizer/model_plugins/poro.rb | 4 ++++ lib/typelizer/property.rb | 2 +- lib/typelizer/templates/interface.ts.erb | 1 + spec/__snapshots__/AlbaInline.ts.snap | 2 +- spec/__snapshots__/AlbaMeta.ts.snap | 2 +- spec/__snapshots__/AlbaMetaNil.ts.snap | 2 +- spec/__snapshots__/AlbaPoro.ts.snap | 2 +- spec/__snapshots__/AlbaPost.ts.snap | 2 +- spec/__snapshots__/AlbaUser.ts.snap | 2 +- spec/__snapshots__/AlbaUserAuthor.ts.snap | 3 ++- spec/__snapshots__/AlbaUserEmptyNested.ts.snap | 3 ++- spec/__snapshots__/AlbaUserSerializerFoo.ts.snap | 2 +- spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap | 2 +- spec/__snapshots__/AmsPost.ts.snap | 2 +- spec/__snapshots__/AmsUser.ts.snap | 2 +- spec/__snapshots__/AmsUserAuthor.ts.snap | 3 ++- spec/__snapshots__/AmsUserEmptyNested.ts.snap | 3 ++- spec/__snapshots__/AmsUserSerializerFoo.ts.snap | 2 +- spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap | 2 +- spec/__snapshots__/OjSerializersFlatUser.ts.snap | 2 +- spec/__snapshots__/OjSerializersPost.ts.snap | 2 +- spec/__snapshots__/OjSerializersUser.ts.snap | 2 +- spec/__snapshots__/OjSerializersUserAuthor.ts.snap | 3 ++- spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap | 3 ++- spec/__snapshots__/OjSerializersUserSerializerFoo.ts.snap | 2 +- .../OjSerializersVerbatimModuleSyntax.ts.snap | 2 +- spec/app/app/serializers/alba/user/author_serializer.rb | 2 +- spec/app/app/serializers/ams/user/author_serializer.rb | 2 +- .../serializers/oj_serializers/user/author_serializer.rb | 2 +- spec/app/config/initializers/typelizer.rb | 2 ++ 33 files changed, 55 insertions(+), 28 deletions(-) diff --git a/lib/typelizer/config.rb b/lib/typelizer/config.rb index 2e028f5..44b64f5 100644 --- a/lib/typelizer/config.rb +++ b/lib/typelizer/config.rb @@ -25,6 +25,7 @@ class Config < Struct.new( :types_import_path, :types_global, :verbatim_module_syntax, + :comments, keyword_init: true ) do class << self @@ -46,6 +47,7 @@ def instance type_mapping: TYPE_MAPPING, null_strategy: :nullable, + comments: false, output_dir: js_root.join("types/serializers"), diff --git a/lib/typelizer/interface.rb b/lib/typelizer/interface.rb index 960b57b..0b68f24 100644 --- a/lib/typelizer/interface.rb +++ b/lib/typelizer/interface.rb @@ -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) diff --git a/lib/typelizer/model_plugins/active_record.rb b/lib/typelizer/model_plugins/active_record.rb index a653b6e..6931290 100644 --- a/lib/typelizer/model_plugins/active_record.rb +++ b/lib/typelizer/model_plugins/active_record.rb @@ -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 diff --git a/lib/typelizer/model_plugins/poro.rb b/lib/typelizer/model_plugins/poro.rb index 382774a..cc0857f 100644 --- a/lib/typelizer/model_plugins/poro.rb +++ b/lib/typelizer/model_plugins/poro.rb @@ -8,6 +8,10 @@ def initialize(...) def infer_types(prop) prop end + + def comment_for(prop) + nil + end end end end diff --git a/lib/typelizer/property.rb b/lib/typelizer/property.rb index e647e96..125c78b 100644 --- a/lib/typelizer/property.rb +++ b/lib/typelizer/property.rb @@ -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 diff --git a/lib/typelizer/templates/interface.ts.erb b/lib/typelizer/templates/interface.ts.erb index 9e766a1..57f8795 100644 --- a/lib/typelizer/templates/interface.ts.erb +++ b/lib/typelizer/templates/interface.ts.erb @@ -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 -%> } diff --git a/spec/__snapshots__/AlbaInline.ts.snap b/spec/__snapshots__/AlbaInline.ts.snap index d0a6033..c45afa9 100644 --- a/spec/__snapshots__/AlbaInline.ts.snap +++ b/spec/__snapshots__/AlbaInline.ts.snap @@ -1,4 +1,4 @@ -// Typelizer digest 31c711d30b829018bdc69e3e09ca8e72 +// Typelizer digest f5275d68218cedacb480ce36d1e6bbe2 // // DO NOT MODIFY: This file was automatically generated by Typelizer. diff --git a/spec/__snapshots__/AlbaMeta.ts.snap b/spec/__snapshots__/AlbaMeta.ts.snap index 7d9ace3..c466dbe 100644 --- a/spec/__snapshots__/AlbaMeta.ts.snap +++ b/spec/__snapshots__/AlbaMeta.ts.snap @@ -1,4 +1,4 @@ -// Typelizer digest 324c9ad635869485fb8231dfbed8cffe +// Typelizer digest bf4b0c896fc166774c2208b110c6d2e9 // // DO NOT MODIFY: This file was automatically generated by Typelizer. diff --git a/spec/__snapshots__/AlbaMetaNil.ts.snap b/spec/__snapshots__/AlbaMetaNil.ts.snap index 0b2b612..2dbfd0b 100644 --- a/spec/__snapshots__/AlbaMetaNil.ts.snap +++ b/spec/__snapshots__/AlbaMetaNil.ts.snap @@ -1,4 +1,4 @@ -// Typelizer digest 22be4653e9abf0c8b5e2963602fbf179 +// Typelizer digest aa6e6401d09df2e398c69ccd17c99ee5 // // DO NOT MODIFY: This file was automatically generated by Typelizer. diff --git a/spec/__snapshots__/AlbaPoro.ts.snap b/spec/__snapshots__/AlbaPoro.ts.snap index 56b16d0..b5aa284 100644 --- a/spec/__snapshots__/AlbaPoro.ts.snap +++ b/spec/__snapshots__/AlbaPoro.ts.snap @@ -1,4 +1,4 @@ -// Typelizer digest 3c3acd1390157d9666f0f2be46bfcb1c +// Typelizer digest 4a7987ac34502a3281b1f42e3f266099 // // DO NOT MODIFY: This file was automatically generated by Typelizer. diff --git a/spec/__snapshots__/AlbaPost.ts.snap b/spec/__snapshots__/AlbaPost.ts.snap index bc55c7f..a0ba35a 100644 --- a/spec/__snapshots__/AlbaPost.ts.snap +++ b/spec/__snapshots__/AlbaPost.ts.snap @@ -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' diff --git a/spec/__snapshots__/AlbaUser.ts.snap b/spec/__snapshots__/AlbaUser.ts.snap index de9ae0e..008e61a 100644 --- a/spec/__snapshots__/AlbaUser.ts.snap +++ b/spec/__snapshots__/AlbaUser.ts.snap @@ -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' diff --git a/spec/__snapshots__/AlbaUserAuthor.ts.snap b/spec/__snapshots__/AlbaUserAuthor.ts.snap index 6ec8281..80b84af 100644 --- a/spec/__snapshots__/AlbaUserAuthor.ts.snap +++ b/spec/__snapshots__/AlbaUserAuthor.ts.snap @@ -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; avatar: unknown; diff --git a/spec/__snapshots__/AlbaUserEmptyNested.ts.snap b/spec/__snapshots__/AlbaUserEmptyNested.ts.snap index 57a4cb0..50c4119 100644 --- a/spec/__snapshots__/AlbaUserEmptyNested.ts.snap +++ b/spec/__snapshots__/AlbaUserEmptyNested.ts.snap @@ -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; avatar: unknown; diff --git a/spec/__snapshots__/AlbaUserSerializerFoo.ts.snap b/spec/__snapshots__/AlbaUserSerializerFoo.ts.snap index 72c82aa..a663b69 100644 --- a/spec/__snapshots__/AlbaUserSerializerFoo.ts.snap +++ b/spec/__snapshots__/AlbaUserSerializerFoo.ts.snap @@ -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' diff --git a/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap b/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap index 01d2a85..111fcc8 100644 --- a/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap +++ b/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap @@ -1,4 +1,4 @@ -// Typelizer digest a6d580654a084cc58071460e42093126 +// Typelizer digest f82208b39a056eaa4fdfc6b8696f7c93 // // DO NOT MODIFY: This file was automatically generated by Typelizer. diff --git a/spec/__snapshots__/AmsPost.ts.snap b/spec/__snapshots__/AmsPost.ts.snap index 4fb07eb..424e82b 100644 --- a/spec/__snapshots__/AmsPost.ts.snap +++ b/spec/__snapshots__/AmsPost.ts.snap @@ -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' diff --git a/spec/__snapshots__/AmsUser.ts.snap b/spec/__snapshots__/AmsUser.ts.snap index 01a7b55..2778662 100644 --- a/spec/__snapshots__/AmsUser.ts.snap +++ b/spec/__snapshots__/AmsUser.ts.snap @@ -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' diff --git a/spec/__snapshots__/AmsUserAuthor.ts.snap b/spec/__snapshots__/AmsUserAuthor.ts.snap index 73a9eb1..4f18894 100644 --- a/spec/__snapshots__/AmsUserAuthor.ts.snap +++ b/spec/__snapshots__/AmsUserAuthor.ts.snap @@ -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; diff --git a/spec/__snapshots__/AmsUserEmptyNested.ts.snap b/spec/__snapshots__/AmsUserEmptyNested.ts.snap index 3c6d503..2e32e44 100644 --- a/spec/__snapshots__/AmsUserEmptyNested.ts.snap +++ b/spec/__snapshots__/AmsUserEmptyNested.ts.snap @@ -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; diff --git a/spec/__snapshots__/AmsUserSerializerFoo.ts.snap b/spec/__snapshots__/AmsUserSerializerFoo.ts.snap index 2ada5b7..553150a 100644 --- a/spec/__snapshots__/AmsUserSerializerFoo.ts.snap +++ b/spec/__snapshots__/AmsUserSerializerFoo.ts.snap @@ -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' diff --git a/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap b/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap index 88f4f3c..0fa4cf2 100644 --- a/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap +++ b/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap @@ -1,4 +1,4 @@ -// Typelizer digest 4f6b6fed16d511dbfe9a02a0b7aa9c41 +// Typelizer digest 7c09585f88197784fbfde340bfd5e36b // // DO NOT MODIFY: This file was automatically generated by Typelizer. diff --git a/spec/__snapshots__/OjSerializersFlatUser.ts.snap b/spec/__snapshots__/OjSerializersFlatUser.ts.snap index ea0e8f6..bce1401 100644 --- a/spec/__snapshots__/OjSerializersFlatUser.ts.snap +++ b/spec/__snapshots__/OjSerializersFlatUser.ts.snap @@ -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' diff --git a/spec/__snapshots__/OjSerializersPost.ts.snap b/spec/__snapshots__/OjSerializersPost.ts.snap index 1240d7f..df285f7 100644 --- a/spec/__snapshots__/OjSerializersPost.ts.snap +++ b/spec/__snapshots__/OjSerializersPost.ts.snap @@ -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' diff --git a/spec/__snapshots__/OjSerializersUser.ts.snap b/spec/__snapshots__/OjSerializersUser.ts.snap index 7113343..19999a6 100644 --- a/spec/__snapshots__/OjSerializersUser.ts.snap +++ b/spec/__snapshots__/OjSerializersUser.ts.snap @@ -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' diff --git a/spec/__snapshots__/OjSerializersUserAuthor.ts.snap b/spec/__snapshots__/OjSerializersUserAuthor.ts.snap index 2e24acd..ea4a8d7 100644 --- a/spec/__snapshots__/OjSerializersUserAuthor.ts.snap +++ b/spec/__snapshots__/OjSerializersUserAuthor.ts.snap @@ -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; avatar: unknown; diff --git a/spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap b/spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap index cf27da9..ddd6009 100644 --- a/spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap +++ b/spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap @@ -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; avatar: unknown; diff --git a/spec/__snapshots__/OjSerializersUserSerializerFoo.ts.snap b/spec/__snapshots__/OjSerializersUserSerializerFoo.ts.snap index 1380206..8510f5f 100644 --- a/spec/__snapshots__/OjSerializersUserSerializerFoo.ts.snap +++ b/spec/__snapshots__/OjSerializersUserSerializerFoo.ts.snap @@ -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' diff --git a/spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap b/spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap index a828e0b..07a6068 100644 --- a/spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap +++ b/spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap @@ -1,4 +1,4 @@ -// Typelizer digest c0f49e6c7ece40d4d23624555e33da67 +// Typelizer digest e4b0b8226d1bf89d94bd35bc366e5837 // // DO NOT MODIFY: This file was automatically generated by Typelizer. diff --git a/spec/app/app/serializers/alba/user/author_serializer.rb b/spec/app/app/serializers/alba/user/author_serializer.rb index 1dcbdc0..0bb4adc 100644 --- a/spec/app/app/serializers/alba/user/author_serializer.rb +++ b/spec/app/app/serializers/alba/user/author_serializer.rb @@ -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? } diff --git a/spec/app/app/serializers/ams/user/author_serializer.rb b/spec/app/app/serializers/ams/user/author_serializer.rb index 66f8b72..f8f3abe 100644 --- a/spec/app/app/serializers/ams/user/author_serializer.rb +++ b/spec/app/app/serializers/ams/user/author_serializer.rb @@ -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? } diff --git a/spec/app/app/serializers/oj_serializers/user/author_serializer.rb b/spec/app/app/serializers/oj_serializers/user/author_serializer.rb index 581fb83..eb38938 100644 --- a/spec/app/app/serializers/oj_serializers/user/author_serializer.rb +++ b/spec/app/app/serializers/oj_serializers/user/author_serializer.rb @@ -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? } diff --git a/spec/app/config/initializers/typelizer.rb b/spec/app/config/initializers/typelizer.rb index ec55ab6..9458f13 100644 --- a/spec/app/config/initializers/typelizer.rb +++ b/spec/app/config/initializers/typelizer.rb @@ -4,4 +4,6 @@ ] c.types_global = %w[Array Date Record] + + c.comments = true end