Fix issues with json files
File parsing no longer uses istream, which strips whitespace. Moved hard-coded json to its own file.
Signed-off-by: Alexander Yee <xander.yee@gmail.com>
Change-Id: I342b74fd1cc8c5af7721af5955aa7d7b185ac71e
diff --git a/aos/BUILD b/aos/BUILD
index b8468bd..0792e67 100644
--- a/aos/BUILD
+++ b/aos/BUILD
@@ -411,9 +411,11 @@
],
data = [
":json_to_flatbuffer_fbs_reflection_out",
+ ":json_to_flatbuffer_test_spaces.json",
],
target_compatible_with = ["@platforms//os:linux"],
deps = [
+ ":flatbuffer_merge",
":json_to_flatbuffer",
":json_to_flatbuffer_fbs",
"//aos/testing:googletest",
diff --git a/aos/json_to_flatbuffer.h b/aos/json_to_flatbuffer.h
index d5e1039..6eab22a 100644
--- a/aos/json_to_flatbuffer.h
+++ b/aos/json_to_flatbuffer.h
@@ -109,10 +109,8 @@
template <typename T>
inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
const std::string_view path) {
- std::ifstream t{std::string(path)};
- std::istream_iterator<char> start(t), end;
- std::string result(start, end);
- return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
+ return FlatbufferDetachedBuffer<T>(
+ JsonToFlatbuffer<T>(util::ReadFileToStringOrDie(path)));
}
// Parses a file as a binary flatbuffer or dies.
diff --git a/aos/json_to_flatbuffer_test.cc b/aos/json_to_flatbuffer_test.cc
index 6772826..fddb431 100644
--- a/aos/json_to_flatbuffer_test.cc
+++ b/aos/json_to_flatbuffer_test.cc
@@ -1,5 +1,6 @@
#include "aos/json_to_flatbuffer.h"
+#include "aos/flatbuffer_merge.h"
#include "aos/json_to_flatbuffer_generated.h"
#include "aos/testing/path.h"
#include "flatbuffers/minireflect.h"
@@ -301,5 +302,20 @@
ConfigurationTypeTable()));
}
+TEST_F(JsonToFlatbufferTest, SpacedData) {
+ EXPECT_TRUE(CompareFlatBuffer(
+ FlatbufferDetachedBuffer<VectorOfStrings>(
+ JsonToFlatbuffer<VectorOfStrings>(R"json({
+ "str": [
+ "f o o",
+ "b a r",
+ "foo bar",
+ "bar foo"
+ ]
+})json")),
+ JsonFileToFlatbuffer<VectorOfStrings>(
+ ArtifactPath("aos/json_to_flatbuffer_test_spaces.json"))));
+}
+
} // namespace testing
} // namespace aos
diff --git a/aos/json_to_flatbuffer_test_spaces.json b/aos/json_to_flatbuffer_test_spaces.json
new file mode 100644
index 0000000..c864037
--- /dev/null
+++ b/aos/json_to_flatbuffer_test_spaces.json
@@ -0,0 +1,8 @@
+{
+ "str": [
+ "f o o",
+ "b a r",
+ "foo bar",
+ "bar foo"
+ ]
+}
diff --git a/aos/starter/BUILD b/aos/starter/BUILD
index 5690bda..8d3f4f5 100644
--- a/aos/starter/BUILD
+++ b/aos/starter/BUILD
@@ -226,6 +226,9 @@
srcs = [
"irq_affinity.cc",
],
+ data = [
+ "//aos/starter:rockpi_config.json",
+ ],
visibility = ["//visibility:public"],
deps = [
":irq_affinity_lib",
diff --git a/aos/starter/irq_affinity.cc b/aos/starter/irq_affinity.cc
index 3f32ec9..5c70e9e 100644
--- a/aos/starter/irq_affinity.cc
+++ b/aos/starter/irq_affinity.cc
@@ -14,6 +14,8 @@
DEFINE_string(user, "",
"Starter runs as though this user ran a SUID binary if set.");
+DEFINE_string(irq_config, "rockpi_config.json",
+ "File path of rockpi configuration");
namespace aos {
@@ -265,117 +267,10 @@
aos::FlatbufferDetachedBuffer<aos::Configuration> config =
aos::configuration::ReadConfig(FLAGS_config);
- // TODO(austin): File instead of hard-coded JSON.
aos::FlatbufferDetachedBuffer<aos::starter::IrqAffinityConfig>
irq_affinity_config =
- aos::JsonToFlatbuffer<aos::starter::IrqAffinityConfig>(
- R"json({
- "irqs": [
- {
- "name": "ttyS2",
- "affinity": [1]
- },
- {
- "name": "dw-mci",
- "affinity": [1]
- },
- {
- "name": "mmc1",
- "affinity": [1]
- },
- {
- "name": "rkisp1",
- "affinity": [2]
- },
- {
- "name": "ff3c0000.i2c",
- "affinity": [2]
- },
- {
- "name": "ff3d0000.i2c",
- "affinity": [2]
- },
- {
- "name": "ff6e0000.dma-controller",
- "affinity": [0]
- },
- {
- "name": "ff1d0000.spi",
- "affinity": [0]
- },
- {
- "name": "eth0",
- "affinity": [1]
- }
- ],
- "kthreads": [
- {
- "name": "irq/*-ff940000.hdmi",
- "scheduler": "SCHEDULER_OTHER"
- },
- {
- "name": "irq/*-rockchip_usb2phy",
- "scheduler": "SCHEDULER_OTHER"
- },
- {
- "name": "irq/*-mmc0",
- "scheduler": "SCHEDULER_OTHER"
- },
- {
- "name": "irq/*-mmc1",
- "scheduler": "SCHEDULER_OTHER"
- },
- {
- "name": "irq/*-fe320000.mmc cd",
- "scheduler": "SCHEDULER_OTHER"
- },
- {
- "name": "irq/*-vc4 crtc",
- "scheduler": "SCHEDULER_OTHER"
- },
- {
- "name": "irq/*-rkisp1",
- "scheduler": "SCHEDULER_FIFO",
- "priority": 60,
- "affinity": [2]
- },
- {
- "name": "irq/*-ff3c0000.i2c",
- "scheduler": "SCHEDULER_FIFO",
- "priority": 51,
- "affinity": [2]
- },
- {
- "name": "irq/*-adis16505",
- "scheduler": "SCHEDULER_FIFO",
- "priority": 59,
- "affinity": [0]
- },
- {
- "name": "irq/*-ff6e0000.dma-controller",
- "scheduler": "SCHEDULER_FIFO",
- "priority": 59,
- "affinity": [0]
- },
- {
- "name": "spi0",
- "scheduler": "SCHEDULER_FIFO",
- "priority": 57,
- "affinity": [0]
- },
- {
- "name": "irq/*-eth0",
- "scheduler": "SCHEDULER_FIFO",
- "priority": 10,
- "affinity": [1]
- },
- {
- "name": "irq/*-rockchip_thermal",
- "scheduler": "SCHEDULER_FIFO",
- "priority": 1
- }
- ]
-})json");
+ aos::JsonFileToFlatbuffer<aos::starter::IrqAffinityConfig>(
+ FLAGS_irq_config);
aos::ShmEventLoop shm_event_loop(&config.message());
diff --git a/aos/starter/rockpi_config.json b/aos/starter/rockpi_config.json
new file mode 100644
index 0000000..d4cbaae
--- /dev/null
+++ b/aos/starter/rockpi_config.json
@@ -0,0 +1,107 @@
+{
+ "irqs": [
+ {
+ "name": "ttyS2",
+ "affinity": [1]
+ },
+ {
+ "name": "dw-mci",
+ "affinity": [1]
+ },
+ {
+ "name": "mmc1",
+ "affinity": [1]
+ },
+ {
+ "name": "rkisp1",
+ "affinity": [2]
+ },
+ {
+ "name": "ff3c0000.i2c",
+ "affinity": [2]
+ },
+ {
+ "name": "ff3d0000.i2c",
+ "affinity": [2]
+ },
+ {
+ "name": "ff6e0000.dma-controller",
+ "affinity": [0]
+ },
+ {
+ "name": "ff1d0000.spi",
+ "affinity": [0]
+ },
+ {
+ "name": "eth0",
+ "affinity": [1]
+ }
+ ],
+ "kthreads": [
+ {
+ "name": "irq/*-ff940000.hdmi",
+ "scheduler": "SCHEDULER_OTHER"
+ },
+ {
+ "name": "irq/*-rockchip_usb2phy",
+ "scheduler": "SCHEDULER_OTHER"
+ },
+ {
+ "name": "irq/*-mmc0",
+ "scheduler": "SCHEDULER_OTHER"
+ },
+ {
+ "name": "irq/*-mmc1",
+ "scheduler": "SCHEDULER_OTHER"
+ },
+ {
+ "name": "irq/*-fe320000.mmc cd",
+ "scheduler": "SCHEDULER_OTHER"
+ },
+ {
+ "name": "irq/*-vc4 crtc",
+ "scheduler": "SCHEDULER_OTHER"
+ },
+ {
+ "name": "irq/*-rkisp1",
+ "scheduler": "SCHEDULER_FIFO",
+ "priority": 60,
+ "affinity": [2]
+ },
+ {
+ "name": "irq/*-ff3c0000.i2c",
+ "scheduler": "SCHEDULER_FIFO",
+ "priority": 51,
+ "affinity": [2]
+ },
+ {
+ "name": "irq/*-adis16505",
+ "scheduler": "SCHEDULER_FIFO",
+ "priority": 59,
+ "affinity": [0]
+ },
+ {
+ "name": "irq/*-ff6e0000.dma-controller",
+ "scheduler": "SCHEDULER_FIFO",
+ "priority": 59,
+ "affinity": [0]
+ },
+ {
+ "name": "spi0",
+ "scheduler": "SCHEDULER_FIFO",
+ "priority": 57,
+ "affinity": [0]
+ },
+ {
+ "name": "irq/*-eth0",
+ "scheduler": "SCHEDULER_FIFO",
+ "priority": 10,
+ "affinity": [1]
+ },
+ {
+ "name": "irq/*-rockchip_thermal",
+ "scheduler": "SCHEDULER_FIFO",
+ "priority": 1
+ }
+ ]
+}
diff --git a/frc971/constants/BUILD b/frc971/constants/BUILD
index c17dc31..3a7e73a 100644
--- a/frc971/constants/BUILD
+++ b/frc971/constants/BUILD
@@ -45,6 +45,7 @@
],
data = [
"//frc971/constants/testdata:aos_config",
+ "//frc971/constants/testdata:syntax_error.json",
"//frc971/constants/testdata:test_constants.json",
],
target_compatible_with = ["@platforms//os:linux"],
diff --git a/frc971/constants/constants_sender_test.cc b/frc971/constants/constants_sender_test.cc
index 1441767..512cb68 100644
--- a/frc971/constants/constants_sender_test.cc
+++ b/frc971/constants/constants_sender_test.cc
@@ -113,11 +113,11 @@
({
ConstantSender<testdata::ConstantsData, testdata::ConstantsList>
test_syntax(constants_sender_event_loop_.get(),
- "frc971/constants/testdata/syntaxerror.json", 971,
+ "frc971/constants/testdata/syntax_error.json", 971,
"/constants");
event_loop_factory_.RunFor(std::chrono::seconds(1));
}),
- "Error on line 0");
+ "Invalid field name");
}
} // namespace testing
diff --git a/frc971/constants/testdata/BUILD b/frc971/constants/testdata/BUILD
index 04cf8d3..a20264c 100644
--- a/frc971/constants/testdata/BUILD
+++ b/frc971/constants/testdata/BUILD
@@ -1,7 +1,10 @@
load("//aos:config.bzl", "aos_config")
load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
-exports_files(["test_constants.json"])
+exports_files([
+ "test_constants.json",
+ "syntax_error.json",
+])
flatbuffer_cc_library(
name = "constants_list_fbs",