Debian Codegraph @

cpp/libjsonnet++.cpp

raw
1 /*
2 Copyright 2015 Google Inc. All rights reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 #include "libjsonnet++.h"
18
19 namespace jsonnet {
20 Jsonnet::Jsonnet(): vm_(nullptr) {}
21
22 Jsonnet::~Jsonnet()
23 {
24 if (vm_ != nullptr) {
25 ::jsonnet_destroy(vm_);
26 }
27 }
28
29 /* static */
30 std::string Jsonnet::version()
31 {
32 return ::jsonnet_version();
33 }
34
35 bool Jsonnet::init()
36 {
37 vm_ = static_cast<struct JsonnetVm*>(::jsonnet_make());
38 return vm_ != nullptr;
39 }
40
41 void Jsonnet::setMaxStack(uint32_t depth)
42 {
43 ::jsonnet_max_stack(vm_, static_cast<unsigned>(depth));
44 }
45
46 void Jsonnet::setGcMinObjects(uint32_t objects)
47 {
48 ::jsonnet_gc_min_objects(vm_, static_cast<unsigned>(objects));
49 }
50
51 void Jsonnet::setGcGrowthTrigger(double growth)
52 {
53 ::jsonnet_gc_growth_trigger(vm_, growth);
54 }
55
56 void Jsonnet::setStringOutput(bool string_output)
57 {
58 ::jsonnet_string_output(vm_, string_output);
59 }
60
61 void Jsonnet::setTrailingNewline(bool enable)
62 {
63 ::jsonnet_set_trailing_newline(vm_, enable);
64 }
65
66 void Jsonnet::addImportPath(const std::string& path)
67 {
68 ::jsonnet_jpath_add(vm_, path.c_str());
69 }
70
71 void Jsonnet::setMaxTrace(uint32_t lines)
72 {
73 ::jsonnet_max_trace(vm_, static_cast<unsigned>(lines));
74 }
75
76 void Jsonnet::bindTlaVar(const std::string& key, const std::string& value)
77 {
78 ::jsonnet_tla_var(vm_, key.c_str(), value.c_str());
79 }
80
81 void Jsonnet::bindTlaCodeVar(const std::string& key, const std::string& value)
82 {
83 ::jsonnet_tla_code(vm_, key.c_str(), value.c_str());
84 }
85
86 void Jsonnet::bindExtVar(const std::string& key, const std::string& value)
87 {
88 ::jsonnet_ext_var(vm_, key.c_str(), value.c_str());
89 }
90
91 void Jsonnet::bindExtCodeVar(const std::string& key, const std::string& value)
92 {
93 ::jsonnet_ext_code(vm_, key.c_str(), value.c_str());
94 }
95
96 bool Jsonnet::evaluateFile(const std::string& filename, std::string* output)
97 {
98 if (output == nullptr) {
99 return false;
100 }
101 int error = 0;
102 char* jsonnet_output = ::jsonnet_evaluate_file(vm_, filename.c_str(), &error);
103 if (error != 0) {
104 last_error_.assign(jsonnet_output);
105 jsonnet_realloc(vm_, jsonnet_output, 0);
106 return false;
107 }
108 output->assign(jsonnet_output);
109 jsonnet_realloc(vm_, jsonnet_output, 0);
110 return true;
111 }
112
113 bool Jsonnet::evaluateSnippet(const std::string& filename, const std::string& snippet,
114 std::string* output)
115 {
116 if (output == nullptr) {
117 return false;
118 }
119 int error = 0;
120 char* jsonnet_output =
121 ::jsonnet_evaluate_snippet(vm_, filename.c_str(), snippet.c_str(), &error);
122 if (error != 0) {
123 last_error_.assign(jsonnet_output);
124 jsonnet_realloc(vm_, jsonnet_output, 0);
125 return false;
126 }
127 output->assign(jsonnet_output);
128 jsonnet_realloc(vm_, jsonnet_output, 0);
129 return true;
130 }
131
132 namespace {
133 void parseMultiOutput(const char* jsonnet_output, std::map<std::string, std::string>* outputs)
134 {
135 for (const char* c = jsonnet_output; *c != '\0';) {
136 const char* filename = c;
137 const char* c2 = c;
138 while (*c2 != '\0')
139 ++c2;
140 ++c2;
141 const char* json = c2;
142 while (*c2 != '\0')
143 ++c2;
144 ++c2;
145 c = c2;
146 outputs->insert(std::make_pair(filename, json));
147 }
148 }
149 } // namespace
150
151 bool Jsonnet::evaluateFileMulti(const std::string& filename,
152 std::map<std::string, std::string>* outputs)
153 {
154 if (outputs == nullptr) {
155 return false;
156 }
157 int error = 0;
158 char* jsonnet_output = ::jsonnet_evaluate_file_multi(vm_, filename.c_str(), &error);
159 if (error != 0) {
160 last_error_.assign(jsonnet_output);
161 jsonnet_realloc(vm_, jsonnet_output, 0);
162 return false;
163 }
164 parseMultiOutput(jsonnet_output, outputs);
165 jsonnet_realloc(vm_, jsonnet_output, 0);
166 return true;
167 }
168
169 bool Jsonnet::evaluateSnippetMulti(const std::string& filename, const std::string& snippet,
170 std::map<std::string, std::string>* outputs)
171 {
172 if (outputs == nullptr) {
173 return false;
174 }
175 int error = 0;
176 char* jsonnet_output =
177 ::jsonnet_evaluate_snippet_multi(vm_, filename.c_str(), snippet.c_str(), &error);
178 if (error != 0) {
179 last_error_.assign(jsonnet_output);
180 jsonnet_realloc(vm_, jsonnet_output, 0);
181 return false;
182 }
183 parseMultiOutput(jsonnet_output, outputs);
184 jsonnet_realloc(vm_, jsonnet_output, 0);
185 return true;
186 }
187
188 std::string Jsonnet::lastError() const
189 {
190 return last_error_;
191 }
192
193 } // namespace jsonnet