foobara-typescript-remote-command-generator 1.1.6 → 1.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ded6d4d4e950f079230273d922ef380455597c4dd0fcf01005396156871291e
4
- data.tar.gz: 9f2ff8044bd23a32c5db7768a4783e17c3be1742f43edf7d6c60664935e9ff1f
3
+ metadata.gz: 9f783af32070cfcdc33c70d34c1b755a66a0a6bb3df7b7e8697345bdc1d148f7
4
+ data.tar.gz: fdcfa71700d34b3c42163b586fc2f8cb1c90f420bef3fb76f7a8a5a8d29e6423
5
5
  SHA512:
6
- metadata.gz: 4447f6d43c30e80f514c8db191bbf8bb115ded65df4febaa030c877bffd88db7820db66d637185353e7bb7aab81d666dc38002d5413d99358f5a07e217fdcbfc
7
- data.tar.gz: 4929a9a0ccb737f0df18979adcfc7d3d223c272c810f44aef208441b01077b77fcde6a114e0f822347530a39774c5bd06b0d2463ddb6503835a18d3853ea7449
6
+ metadata.gz: b5b5d35cc80f88622f2a4ea321574a7af55e9c61ec7bde32d42761a9d9511af07ced97894f4905962bb1803c1871d9a8ab4c9c83722df5caefffe7342b90e474
7
+ data.tar.gz: 6d0a1fcb787dc719a330bd58953d42e387f931b35a2eb3d6168ab0d0dfbdec166a1f2215339ea7046b3edfc00473365cdca75e6846408014ed96a3f04d6ef3d8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## [1.2.0] - 2025-11-06
2
+
3
+ - If an attribute isn't required but has a default, it will have a non-required create
4
+ interface but a required read interface. This updates the types to reflect that for
5
+ convenience to avoid pointless null checks.
6
+
7
+ ## [1.1.7] - 2025-11-02
8
+
9
+ - Make CommandCastResultGenerator's interpretation of atom? match other generators
10
+
1
11
  ## [1.1.6] - 2025-10-17
2
12
 
3
13
  - Dirty all queries on login/logout, not just GetCurrentUser
@@ -5,12 +5,12 @@ module Foobara
5
5
  class Services
6
6
  class CommandCastResultGenerator < CommandResultGenerator
7
7
  class CastTree
8
- attr_accessor :children, :declaration_to_cast, :past_first_model
8
+ attr_accessor :children, :declaration_to_cast, :initial
9
9
 
10
- def initialize(children: nil, declaration_to_cast: nil, past_first_model: false)
10
+ def initialize(children: nil, declaration_to_cast: nil, initial: false)
11
11
  self.children = children
12
12
  self.declaration_to_cast = declaration_to_cast
13
- self.past_first_model = past_first_model
13
+ self.initial = initial
14
14
  end
15
15
 
16
16
  def empty?
@@ -41,31 +41,30 @@ module Foobara
41
41
 
42
42
  nested_model_generators = []
43
43
 
44
- generators = model_generators
45
-
46
- generators.each do |generator|
47
- _models_reachable_from_declaration(
48
- generator.relevant_manifest
49
- )&.each do |(model, past_first)|
50
- generator_class = if atom?
51
- if model.detached_entity? && past_first
52
- Services::UnloadedEntityGenerator
53
- else
54
- Services::AtomModelGenerator
55
- end
56
- elsif aggregate?
57
- Services::AggregateModelGenerator
44
+ if result_type.detached_entity? && atom?
45
+ declaration = result_type.is_a?(Manifest::TypeDeclaration) ? result_type.to_type : result_type
46
+ return @nested_model_generators = Set[Services::AtomEntityGenerator.new(declaration)]
47
+ end
48
+
49
+ _models_reachable_from_declaration(result_type, initial: true)&.each do |model|
50
+ generator_class = if atom?
51
+ if model.detached_entity?
52
+ Services::UnloadedEntityGenerator
58
53
  else
59
- Services::ModelGenerator
54
+ Services::AtomModelGenerator
60
55
  end
56
+ elsif aggregate?
57
+ Services::AggregateModelGenerator
58
+ else
59
+ Services::ModelGenerator
60
+ end
61
61
 
62
- new_generator = generator_class.new(model)
62
+ new_generator = generator_class.new(model)
63
63
 
64
- unless generators.any? do |g|
65
- g.relevant_manifest == model && g.class == new_generator.class
66
- end
67
- nested_model_generators << new_generator
68
- end
64
+ unless nested_model_generators.any? do |g|
65
+ g.relevant_manifest == model && g.class == new_generator.class
66
+ end
67
+ nested_model_generators << new_generator
69
68
  end
70
69
  end
71
70
 
@@ -81,7 +80,7 @@ module Foobara
81
80
  end
82
81
 
83
82
  def dependencies
84
- @dependencies ||= model_generators + nested_model_generators
83
+ nested_model_generators
85
84
  end
86
85
 
87
86
  def cast_json_result_function
@@ -92,7 +91,7 @@ module Foobara
92
91
 
93
92
  # TODO: need to make use of initial?
94
93
  def cast_json_result_function_body(
95
- cast_tree = _construct_cast_tree(result_type),
94
+ cast_tree = _construct_cast_tree(result_type, initial: true),
96
95
  parent: "json",
97
96
  property: nil,
98
97
  value: parent
@@ -189,8 +188,9 @@ module Foobara
189
188
  if type_symbol == :date || type_symbol == :datetime
190
189
  "#{lvalue} = new Date(#{value})"
191
190
  elsif type.model?
192
- ts_model_name = model_to_ts_model_name(type, association_depth:,
193
- initial: !cast_tree.past_first_model)
191
+ ts_model_name = model_to_ts_model_name(type,
192
+ association_depth:,
193
+ initial: cast_tree.initial)
194
194
 
195
195
  "#{lvalue} = new #{ts_model_name}(#{value})"
196
196
  else
@@ -200,7 +200,7 @@ module Foobara
200
200
  end
201
201
  end
202
202
 
203
- def _construct_cast_tree(type_declaration, past_first_model: false)
203
+ def _construct_cast_tree(type_declaration, initial: false)
204
204
  if type_declaration.is_a?(Manifest::Attributes)
205
205
  return unless type_declaration.has_attribute_declarations?
206
206
  return if type_declaration.attribute_declarations.empty?
@@ -209,39 +209,39 @@ module Foobara
209
209
 
210
210
  type_declaration.attribute_declarations.each_pair do |attribute_name, attribute_declaration|
211
211
  if type_requires_cast?(attribute_declaration)
212
- path_tree[attribute_name] = _construct_cast_tree(attribute_declaration, past_first_model:)
212
+ path_tree[attribute_name] = _construct_cast_tree(attribute_declaration)
213
213
  end
214
214
  end
215
215
 
216
216
  unless path_tree.empty?
217
- CastTree.new(children: path_tree, past_first_model:)
217
+ CastTree.new(children: path_tree, initial:)
218
218
  end
219
219
  elsif type_declaration.is_a?(Manifest::Array)
220
220
  element_type = type_declaration.element_type
221
221
 
222
222
  if element_type && type_requires_cast?(element_type)
223
- CastTree.new(children: { "#": _construct_cast_tree(element_type, past_first_model:) })
223
+ CastTree.new(initial:, children: { "#": _construct_cast_tree(element_type) })
224
224
  end
225
225
  elsif type_declaration.type.to_sym == :date || type_declaration.type.to_sym == :datetime
226
- CastTree.new(declaration_to_cast: type_declaration, past_first_model:)
226
+ CastTree.new(declaration_to_cast: type_declaration, initial:)
227
227
  elsif type_declaration.model?
228
228
  type_declaration = type_declaration.to_type
229
229
 
230
230
  children = if type_declaration.detached_entity? && atom?
231
231
  nil
232
232
  else
233
- _construct_cast_tree(type_declaration.attributes_type, past_first_model: true)
233
+ _construct_cast_tree(type_declaration.attributes_type)
234
234
  end
235
235
 
236
- CastTree.new(children:, declaration_to_cast: type_declaration, past_first_model:)
236
+ CastTree.new(children:, declaration_to_cast: type_declaration, initial:)
237
237
  # TODO: either test this code path or raise or delete it
238
238
  # :nocov:
239
239
  elsif type_declaration.custom?
240
240
  if type_requires_cast?(type_declaration.base_type.to_type_declaration)
241
- tree = _construct_cast_tree(type_declaration.base_type.to_type_declaration, past_first_model:)
241
+ tree = _construct_cast_tree(type_declaration.base_type.to_type_declaration)
242
242
 
243
243
  if tree && !tree.empty?
244
- CastTree.new(children: tree, past_first_model:)
244
+ CastTree.new(children: tree, initial:)
245
245
  end
246
246
  end
247
247
  end
@@ -249,7 +249,7 @@ module Foobara
249
249
  end
250
250
 
251
251
  # TODO: Feels like similar complicated logic is popping up in many places? How to find/converge such logic
252
- def _models_reachable_from_declaration(type_declaration, past_first_model: false)
252
+ def _models_reachable_from_declaration(type_declaration, initial: false)
253
253
  if type_declaration.is_a?(Manifest::Attributes)
254
254
  return unless type_declaration.has_attribute_declarations?
255
255
  return if type_declaration.attribute_declarations.empty?
@@ -258,13 +258,14 @@ module Foobara
258
258
 
259
259
  type_declaration.attribute_declarations.each_value do |attribute_declaration|
260
260
  if type_requires_cast?(attribute_declaration)
261
- models ||= Set.new
262
-
263
- _models_reachable_from_declaration(
264
- attribute_declaration,
265
- past_first_model:
266
- )&.each do |pair|
267
- models << pair
261
+ additional = _models_reachable_from_declaration(attribute_declaration)
262
+
263
+ if additional
264
+ if models
265
+ models |= additional
266
+ else
267
+ models = additional
268
+ end
268
269
  end
269
270
  end
270
271
  end
@@ -274,24 +275,23 @@ module Foobara
274
275
  element_type = type_declaration.element_type
275
276
 
276
277
  if element_type && type_requires_cast?(element_type)
277
- _models_reachable_from_declaration(element_type, past_first_model:)
278
+ _models_reachable_from_declaration(element_type)
278
279
  end
279
280
  elsif type_declaration.model?
280
281
  if type_declaration.is_a?(Manifest::TypeDeclaration)
281
282
  type_declaration = type_declaration.to_type
282
283
  end
283
284
 
284
- models = Set[[type_declaration, past_first_model]]
285
+ models = Set[type_declaration]
285
286
 
286
287
  if atom? && type_declaration.detached_entity?
287
288
  return models
288
289
  end
289
290
 
290
- _models_reachable_from_declaration(
291
- type_declaration.attributes_type,
292
- past_first_model: true
293
- )&.each do |pair|
294
- models << pair
291
+ additional = _models_reachable_from_declaration(type_declaration.attributes_type)
292
+
293
+ if additional
294
+ models |= additional
295
295
  end
296
296
 
297
297
  models
@@ -299,10 +299,7 @@ module Foobara
299
299
  # TODO: either test this code path or raise or delete it
300
300
  # :nocov:
301
301
  if type_requires_cast?(type_declaration.base_type.to_type_declaration)
302
- _models_reachable_from_declaration(
303
- type_declaration.base_type.to_type_declaration,
304
- past_first_model:
305
- )
302
+ _models_reachable_from_declaration(type_declaration.base_type.to_type_declaration)
306
303
  end
307
304
  # :nocov:
308
305
  end
@@ -120,17 +120,29 @@ module Foobara
120
120
 
121
121
  def attributes_type_ts_type
122
122
  association_depth = AssociationDepth::AMBIGUOUS
123
- foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
123
+ foobara_type_to_ts_type(attributes_type,
124
+ association_depth:,
125
+ dependency_group:,
126
+ is_output: true,
127
+ parent: relevant_manifest)
124
128
  end
125
129
 
126
130
  def atom_attributes_ts_type
127
131
  association_depth = AssociationDepth::ATOM
128
- foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
132
+ foobara_type_to_ts_type(attributes_type,
133
+ association_depth:,
134
+ dependency_group:,
135
+ is_output: true,
136
+ parent: relevant_manifest)
129
137
  end
130
138
 
131
139
  def aggregate_attributes_ts_type
132
140
  association_depth = AssociationDepth::AGGREGATE
133
- foobara_type_to_ts_type(attributes_type, association_depth:, dependency_group:)
141
+ foobara_type_to_ts_type(attributes_type,
142
+ association_depth:,
143
+ dependency_group:,
144
+ is_output: true,
145
+ parent: relevant_manifest)
134
146
  end
135
147
 
136
148
  def attribute_names
@@ -194,14 +194,19 @@ module Foobara
194
194
  end
195
195
  end
196
196
 
197
- # TODO: relocate this to a more reusable place
197
+ # is_output means the value came from elsewhere and is fully formed.
198
+ # This is helpful for specifying what is expected to be present. If this is provided,
199
+ # then things like attribute properties that have defaults will be considered
200
+ # required and present.
198
201
  def foobara_type_to_ts_type(
199
202
  type_declaration,
200
203
  dependency_group: self.dependency_group,
201
204
  name: nil,
202
205
  association_depth: AssociationDepth::AMBIGUOUS,
203
206
  initial: true,
204
- model_and_entity_free: false
207
+ model_and_entity_free: false,
208
+ is_output: false,
209
+ parent: nil
205
210
  )
206
211
  if type_declaration.is_a?(Manifest::Error)
207
212
  error_generator = generator_for(type_declaration)
@@ -213,7 +218,9 @@ module Foobara
213
218
  type_declaration,
214
219
  association_depth:,
215
220
  dependency_group:,
216
- model_and_entity_free:
221
+ model_and_entity_free:,
222
+ is_output:,
223
+ parent:
217
224
  )
218
225
 
219
226
  if type_declaration.has_attribute_declarations?
@@ -241,7 +248,8 @@ module Foobara
241
248
  association_depth:,
242
249
  dependency_group:,
243
250
  initial: false,
244
- model_and_entity_free:
251
+ model_and_entity_free:,
252
+ is_output:
245
253
  )
246
254
  "#{ts_type}[]"
247
255
  else
@@ -275,7 +283,9 @@ module Foobara
275
283
  association_depth:,
276
284
  dependency_group:,
277
285
  initial:,
278
- model_and_entity_free:
286
+ model_and_entity_free:,
287
+ is_output:,
288
+ parent: model_type
279
289
  )
280
290
  else
281
291
  model_to_ts_model_name(type_declaration, association_depth:, initial:)
@@ -310,20 +320,34 @@ module Foobara
310
320
  attributes,
311
321
  dependency_group:,
312
322
  association_depth: AssociationDepth::AMBIGUOUS,
313
- model_and_entity_free: false
323
+ model_and_entity_free: false,
324
+ is_output: false,
325
+ parent: nil
314
326
  )
315
327
  # TODO: if we don't actually have attribute_declarations because we
316
328
  # are trying to express attributes of any type, then we want Record<string, any>
317
329
  # or something.
318
330
  if attributes.has_attribute_declarations?
319
331
  guts = attributes.attribute_declarations.map do |attribute_name, attribute_declaration|
320
- " #{attribute_name}#{"?" unless attributes.required?(attribute_name)}: #{
332
+ is_required = attributes.required?(attribute_name)
333
+
334
+ if !is_required && is_output
335
+ default = attributes.default_for(attribute_name)
336
+
337
+ if default || default == false ||
338
+ (parent&.detached_entity? && attribute_name == parent.primary_key_name.to_sym)
339
+ is_required = true
340
+ end
341
+ end
342
+
343
+ " #{attribute_name}#{"?" unless is_required}: #{
321
344
  foobara_type_to_ts_type(
322
345
  attribute_declaration,
323
346
  dependency_group:,
324
347
  association_depth:,
325
348
  initial: false,
326
- model_and_entity_free:
349
+ model_and_entity_free:,
350
+ is_output:
327
351
  )
328
352
  }"
329
353
  end.join("\n")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-typescript-remote-command-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi