Skip to content

Commit c677593

Browse files
committed
feat: add ar_comment_to_string config
1 parent b3aaca3 commit c677593

File tree

8 files changed

+22
-27
lines changed

8 files changed

+22
-27
lines changed

lib/anchor/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class Config
99
:use_type_as_schema_name,
1010
:maybe_as_union,
1111
:array_bracket_notation,
12-
:infer_default_as_non_null
12+
:infer_default_as_non_null,
13+
:ar_comment_to_string
1314

1415
def initialize
1516
@ar_column_to_type = nil
@@ -22,6 +23,7 @@ def initialize
2223
@maybe_as_union = nil
2324
@array_bracket_notation = nil
2425
@infer_default_as_non_null = nil
26+
@ar_comment_to_string = nil
2527
end
2628
end
2729
end

lib/anchor/resource.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ def anchor_attributes_properties(included_fields:)
7979
column = !method_defined && _model_class.try(:columns_hash).try(:[], model_method.to_s)
8080
if column
8181
type = Anchor::Types::Inference::ActiveRecord::SQL.from(column)
82-
description ||= column.comment if Anchor.config.use_active_record_comment
82+
unless description
83+
description = column.comment if Anchor.config.use_active_record_comment
84+
if description && !Anchor.config.ar_comment_to_string.nil?
85+
description = Anchor.config.ar_comment_to_string.call(description)
86+
end
87+
end
8388
check_presence = type.is_a?(Anchor::Types::Maybe) && Anchor.config.use_active_record_validations
8489
if check_presence && _model_class.validators_on(model_method).any? do |v|
8590
if v.is_a?(ActiveRecord::Validations::NumericalityValidator)

spec/example/config/initializers/anchor.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ module Anchor
1010
return Types::Literal.new("never") if column.name == "loljk"
1111
Types::Inference::ActiveRecord::SQL.default_ar_column_to_type(column)
1212
}
13+
c.ar_comment_to_string = lambda { |comment|
14+
begin
15+
res = JSON.parse(comment)
16+
res["description"]
17+
rescue JSON::ParserError
18+
comment
19+
end
20+
}
1321

1422
c.empty_relationship_type = -> { Anchor::Types::Object }
1523
end

spec/example/test/files/all_fields_false_schema.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ export type Exhaustive = {
8989
resourceOverridden: unknown;
9090
/** This is a comment. */
9191
withComment: Maybe<string>;
92-
/** {
93-
"description": "This is a parsed JSON comment.",
94-
"test": 2
95-
}
96-
*/
92+
/** This is a parsed JSON comment. */
9793
withParsedComment: Maybe<string>;
9894
defaultedBoolean: boolean;
9995
defaultedAt: string;

spec/example/test/files/excluded_fields_schema.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ export type Exhaustive = {
8787
resourceOverridden: unknown;
8888
/** This is a comment. */
8989
withComment: Maybe<string>;
90-
/** {
91-
"description": "This is a parsed JSON comment.",
92-
"test": 2
93-
}
94-
*/
90+
/** This is a parsed JSON comment. */
9591
withParsedComment: Maybe<string>;
9692
defaultedBoolean: boolean;
9793
defaultedAt: string;

spec/example/test/files/multifile/Exhaustive.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ type Model = {
5454
resourceOverridden: unknown;
5555
/** This is a comment. */
5656
withComment: Maybe<string>;
57-
/** {
58-
"description": "This is a parsed JSON comment.",
59-
"test": 2
60-
}
61-
*/
57+
/** This is a parsed JSON comment. */
6258
withParsedComment: Maybe<string>;
6359
defaultedBoolean: boolean;
6460
defaultedAt: string;

spec/example/test/files/schema.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ export type Exhaustive = {
9696
resourceOverridden: unknown;
9797
/** This is a comment. */
9898
withComment: Maybe<string>;
99-
/** {
100-
"description": "This is a parsed JSON comment.",
101-
"test": 2
102-
}
103-
*/
99+
/** This is a parsed JSON comment. */
104100
withParsedComment: Maybe<string>;
105101
defaultedBoolean: boolean;
106102
defaultedAt: string;

spec/example/test/files/test_schema.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ export type Exhaustive = {
9393
resourceOverridden: unknown;
9494
/** This is a comment. */
9595
withComment: Maybe<string>;
96-
/** {
97-
"description": "This is a parsed JSON comment.",
98-
"test": 2
99-
}
100-
*/
96+
/** This is a parsed JSON comment. */
10197
withParsedComment: Maybe<string>;
10298
defaultedBoolean: boolean;
10399
defaultedAt: string;

0 commit comments

Comments
 (0)