-
Notifications
You must be signed in to change notification settings - Fork 53
Fix dictionary JSON struct default values #931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,17 +169,22 @@ case class DictionaryJsonEncoder( | |
|
|
||
| /** JSON Encoding for arrays */ | ||
| def arrayElementsAsJson(elements: List[Value]): Json = { | ||
| val arrayRes = for(e <- elements) yield { | ||
| val res = e match { | ||
| // Case where array is N-dimensional | ||
| case Value.Array(a, t) => arrayElementsAsJson(a.elements) | ||
| case _ => valueAsJson(e) | ||
| } | ||
|
|
||
| res.asJson | ||
| } | ||
| val arrayRes = for(e <- elements) yield valueAsJson(e) | ||
| arrayRes.asJson | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in issue #925, after further discussion, I think we should revert this change. The current behavior is correct according to the way that FPP represents struct values with member arrays. However, @jwest115 can you make sure that the current F Prime JSON spec is consistent with the FPP model and the generated JSON here? This is a tricky case.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I'm not so sure if we should revert. I think the root cause of the issue is that the dictionary JSON is underspecified here. I opened an issue on this: nasa/fprime#4787. There are at least two ways this could work:
Probably we want option (1), but the JSON spec does not specify what behavior is intended. We should tighten up the JSON spec first, and then make the |
||
| /** JSON Encoding for struct values */ | ||
| def structValueAsJson(value: Value.Struct): Json = { | ||
| val Value.Struct(Value.AnonStruct(members), t) = value | ||
| val Type.Struct(_, _, _, sizes, _) = dictionaryState.a.typeMap(t.node._2.id) | ||
| members.map((key, v) => | ||
| val valueJson = valueAsJson(v) | ||
| sizes.getOrElse(key, 1) match | ||
| case size if size > 1 => | ||
| (key.toString -> Json.fromValues(List.fill(size)(valueJson))) | ||
| case _ => (key.toString -> valueJson) | ||
| ).asJson | ||
| } | ||
|
|
||
| /** JSON Encoding for FPP Values | ||
| * | ||
|
|
@@ -197,7 +202,7 @@ case class DictionaryJsonEncoder( | |
| val qualifiedName = dictionaryState.a.getQualifiedName(Symbol.Enum(t.node)).toString | ||
| s"${qualifiedName}.${v._1}".asJson // FQN of the enum constant | ||
| } | ||
| case Value.Struct(Value.AnonStruct(members), t) => members.map((key, value) => (key.toString -> valueAsJson(value))).asJson | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted, the original code looks more correct here. The new code seems to be a special case to implement a difference between FPP semantics and GDS semantics. I'd rather keep those the same, if possible. |
||
| case v: Value.Struct => structValueAsJson(v) | ||
| case _ => value.toString.asJson | ||
| } | ||
| } | ||
|
|
@@ -280,7 +285,7 @@ case class DictionaryJsonEncoder( | |
| "members" -> membersFormatted.toMap.asJson, | ||
| ) | ||
| val optionalValues = Map( | ||
| "default" -> default, | ||
| "default" -> default.map(structValueAsJson), | ||
| "annotation" -> concatAnnotations(preA, postA) | ||
| ) | ||
| jsonWithOptionalValues(json, optionalValues) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a code improvement (eliminating a special case). Can we keep this change and preserve the existing behavior?