Skip to content

Commit

Permalink
Merge pull request #588 from jwest115/fpp-582
Browse files Browse the repository at this point in the history
Fix JSON dictionary struct member indexes
  • Loading branch information
bocchino authored Jan 30, 2025
2 parents 9b28b28 + cfa6e49 commit f319c72
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,26 +237,25 @@ case class DictionaryJsonEncoder(
jsonWithOptionalValues(json, optionalValues)
}
case Symbol.Struct(preA, node, postA) => {
val Type.Struct(_, anonStruct, default, sizes, formats) = dictionaryState.a.typeMap(symbol.getNodeId)
val Type.AnonStruct(members) = anonStruct
val Type.Struct(_, _, default, sizes, _) = dictionaryState.a.typeMap(symbol.getNodeId)
val memberFormatMap = node.data.members.flatMap { case (_, memberNode, _) =>
memberNode.data.format.map(format => memberNode.data.name -> format.data)
}.toMap
val memberAnnotationMap = node.data.members.flatMap { case (preA, memberNode, postA) =>
val annotation = (preA ++ postA).mkString("\n")
if (annotation.isEmpty) None else Some(memberNode.data.name -> annotation)
}.toMap
val membersFormatted = for(((key, t), index) <- members.zipWithIndex) yield {
val membersFormatted = for(((_, m, _), index) <- node.data.members.zipWithIndex) yield {
val json = Json.obj(
"type" -> typeAsJson(t).asJson,
"type" -> typeAsJson(dictionaryState.a.typeMap(m.data.typeName.id)),
"index" -> index.asJson
)
val optionalValues = Map(
"size" -> sizes.get(key),
"format" -> memberFormatMap.get(key),
"annotation" -> memberAnnotationMap.get(key)
"size" -> sizes.get(m.data.name),
"format" -> memberFormatMap.get(m.data.name),
"annotation" -> memberAnnotationMap.get(m.data.name)
)
(key.toString -> jsonWithOptionalValues(json, optionalValues))
(m.data.name.toString -> jsonWithOptionalValues(json, optionalValues))
}
val json = Json.obj(
"kind" -> "struct".asJson,
Expand Down Expand Up @@ -550,4 +549,4 @@ case class DictionaryJsonEncoder(
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,112 @@
],
"default" : "Module1.E2.PASS"
},
{
"kind" : "struct",
"qualifiedName" : "Module1.ScalarStruct",
"members" : {
"u32" : {
"type" : {
"name" : "U32",
"kind" : "integer",
"size" : 32,
"signed" : false
},
"index" : 6
},
"f64" : {
"type" : {
"name" : "F64",
"kind" : "float",
"size" : 64
},
"index" : 9
},
"f32" : {
"type" : {
"name" : "F32",
"kind" : "float",
"size" : 32
},
"index" : 8
},
"i8" : {
"type" : {
"name" : "I8",
"kind" : "integer",
"size" : 8,
"signed" : true
},
"index" : 0
},
"i16" : {
"type" : {
"name" : "I16",
"kind" : "integer",
"size" : 16,
"signed" : true
},
"index" : 1
},
"u8" : {
"type" : {
"name" : "U8",
"kind" : "integer",
"size" : 8,
"signed" : false
},
"index" : 4
},
"u64" : {
"type" : {
"name" : "U64",
"kind" : "integer",
"size" : 64,
"signed" : false
},
"index" : 7
},
"i64" : {
"type" : {
"name" : "I64",
"kind" : "integer",
"size" : 64,
"signed" : true
},
"index" : 3
},
"i32" : {
"type" : {
"name" : "I32",
"kind" : "integer",
"size" : 32,
"signed" : true
},
"index" : 2
},
"u16" : {
"type" : {
"name" : "U16",
"kind" : "integer",
"size" : 16,
"signed" : false
},
"index" : 5
}
},
"default" : {
"u32" : 0,
"f64" : 0.0,
"f32" : 0.0,
"i8" : 0,
"i16" : 0,
"u8" : 0,
"u64" : 0,
"i64" : 0,
"i32" : 0,
"u16" : 0
}
},
{
"kind" : "struct",
"qualifiedName" : "Module1.S1",
Expand Down Expand Up @@ -253,14 +359,6 @@
],
"annotation" : "Parameter of type string"
},
{
"name" : "Module1.myFirstC1.PARAM1_PRM_SAVE",
"commandKind" : "save",
"opcode" : 773,
"formalParams" : [
],
"annotation" : "Parameter (struct)"
},
{
"name" : "Module1.myFirstC1.PARAM3_PRM_SET",
"commandKind" : "set",
Expand Down Expand Up @@ -405,6 +503,30 @@
],
"annotation" : "Command with 2 args (array of strings and U32)"
},
{
"name" : "Module1.myFirstC2.SendScalars",
"commandKind" : "sync",
"opcode" : 1026,
"formalParams" : [
{
"name" : "s",
"type" : {
"name" : "Module1.ScalarStruct",
"kind" : "qualifiedIdentifier"
},
"ref" : false
}
],
"annotation" : "Send scalars"
},
{
"name" : "Module1.myFirstC1.PARAM1_PRM_SAVE",
"commandKind" : "save",
"opcode" : 773,
"formalParams" : [
],
"annotation" : "Parameter (struct)"
},
{
"name" : "Module1.myFirstC1.PARAM5_PRM_SET",
"commandKind" : "set",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,112 @@
],
"default" : "Module1.E2.PASS"
},
{
"kind" : "struct",
"qualifiedName" : "Module1.ScalarStruct",
"members" : {
"u32" : {
"type" : {
"name" : "U32",
"kind" : "integer",
"size" : 32,
"signed" : false
},
"index" : 6
},
"f64" : {
"type" : {
"name" : "F64",
"kind" : "float",
"size" : 64
},
"index" : 9
},
"f32" : {
"type" : {
"name" : "F32",
"kind" : "float",
"size" : 32
},
"index" : 8
},
"i8" : {
"type" : {
"name" : "I8",
"kind" : "integer",
"size" : 8,
"signed" : true
},
"index" : 0
},
"i16" : {
"type" : {
"name" : "I16",
"kind" : "integer",
"size" : 16,
"signed" : true
},
"index" : 1
},
"u8" : {
"type" : {
"name" : "U8",
"kind" : "integer",
"size" : 8,
"signed" : false
},
"index" : 4
},
"u64" : {
"type" : {
"name" : "U64",
"kind" : "integer",
"size" : 64,
"signed" : false
},
"index" : 7
},
"i64" : {
"type" : {
"name" : "I64",
"kind" : "integer",
"size" : 64,
"signed" : true
},
"index" : 3
},
"i32" : {
"type" : {
"name" : "I32",
"kind" : "integer",
"size" : 32,
"signed" : true
},
"index" : 2
},
"u16" : {
"type" : {
"name" : "U16",
"kind" : "integer",
"size" : 16,
"signed" : false
},
"index" : 5
}
},
"default" : {
"u32" : 0,
"f64" : 0.0,
"f32" : 0.0,
"i8" : 0,
"i16" : 0,
"u8" : 0,
"u64" : 0,
"i64" : 0,
"i32" : 0,
"u16" : 0
}
},
{
"kind" : "struct",
"qualifiedName" : "Module1.S1",
Expand Down Expand Up @@ -283,14 +389,6 @@
],
"annotation" : "Parameter of type string"
},
{
"name" : "Module1.mySecondC1.PARAM1_PRM_SAVE",
"commandKind" : "save",
"opcode" : 1285,
"formalParams" : [
],
"annotation" : "Parameter (struct)"
},
{
"name" : "Module1.mySecondC1.PARAM3_PRM_SET",
"commandKind" : "set",
Expand Down Expand Up @@ -397,6 +495,30 @@
],
"annotation" : "Command with no args"
},
{
"name" : "Module1.mySecondC2.SendScalars",
"commandKind" : "sync",
"opcode" : 1538,
"formalParams" : [
{
"name" : "s",
"type" : {
"name" : "Module1.ScalarStruct",
"kind" : "qualifiedIdentifier"
},
"ref" : false
}
],
"annotation" : "Send scalars"
},
{
"name" : "Module1.mySecondC1.PARAM1_PRM_SAVE",
"commandKind" : "save",
"opcode" : 1285,
"formalParams" : [
],
"annotation" : "Parameter (struct)"
},
{
"name" : "Module1.mySecondC2.Command1",
"commandKind" : "sync",
Expand Down
16 changes: 16 additions & 0 deletions compiler/tools/fpp-to-dict/test/top/multipleTops.fpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ module Module1 {
y: EnumArray
} default { x = E2.PASS }

struct ScalarStruct {
i8: I8,
i16: I16,
i32: I32,
i64: I64,
u8: U8,
u16: U16,
u32: U32,
u64: U64,
f32: F32,
f64: F64
}

# Component
active component Component1 {

Expand Down Expand Up @@ -193,6 +206,9 @@ module Module1 {
@ Command with 3 args (of types string, I32, and bool)
sync command Command2(a: string, b: I32, c: bool)

@ Send scalars
sync command SendScalars(s: ScalarStruct)

# Events
@ Event with a single U64 arg
event Event1(
Expand Down

0 comments on commit f319c72

Please sign in to comment.