Skip to content

Commit

Permalink
Fixed RD-14703: Record.AddField crashing with zero or more than one f…
Browse files Browse the repository at this point in the history
…ield (#517)
  • Loading branch information
bgaidioz authored Oct 1, 2024
1 parent 439c913 commit 215c1c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class RecordPackageTest extends SnapiTestContext {
it should evaluateTo("""Record.Build(b = "Hello", c = "World")""")
}

test("""Record.AddField({a: 1})""")(_ should runErrorAs("new field name and value must be provided"))
test("""Record.AddField({a: 1}, b = 2, c = 3)""")(_ should runErrorAs("only one field can be added"))

test("""
|let r = Record.Build(a = 1, b = "Hello")
|in Record.GetFieldByIndex(r, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,14 @@ class RecordAddFieldEntry extends EntryExtension {
optionalArgs: Seq[(String, Arg)],
varArgs: Seq[Arg]
)(implicit programContext: ProgramContext): Either[String, Type] = {
val SnapiRecordType(atts, _) = mandatoryArgs.head.t
Right(SnapiRecordType(atts ++ optionalArgs.map(a => SnapiAttrType(a._1, a._2.t))))
if (optionalArgs.isEmpty) {
Left("new field name and value must be provided")
} else if (optionalArgs.size > 1) {
Left("only one field can be added")
} else {
val SnapiRecordType(atts, _) = mandatoryArgs.head.t
Right(SnapiRecordType(atts ++ optionalArgs.map(a => SnapiAttrType(a._1, a._2.t))))
}
}

}
Expand Down

0 comments on commit 215c1c4

Please sign in to comment.