|
| 1 | +// Copyright 2015 Google Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "cpp/jsonnet.h" |
| 16 | + |
| 17 | +namespace jsonnet { |
| 18 | + |
| 19 | +Jsonnet::Jsonnet() {} |
| 20 | +Jsonnet::~Jsonnet() { |
| 21 | + if (vm_ != nullptr) { |
| 22 | + ::jsonnet_destroy(vm_); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +bool Jsonnet::Init() { |
| 27 | + vm_ = static_cast<struct JsonnetVm*>(::jsonnet_make()); |
| 28 | + return vm_ != nullptr; |
| 29 | +} |
| 30 | + |
| 31 | +void Jsonnet::SetMaxStack(uint32_t depth) { |
| 32 | + ::jsonnet_max_stack(vm_, static_cast<unsigned>(depth)); |
| 33 | +} |
| 34 | + |
| 35 | +void Jsonnet::SetGcMinObjects(uint32_t objects) { |
| 36 | + ::jsonnet_gc_min_objects(vm_, static_cast<unsigned>(objects)); |
| 37 | +} |
| 38 | + |
| 39 | +void Jsonnet::SetGcGrowthTrigger(double growth) { |
| 40 | + ::jsonnet_gc_growth_trigger(vm_, growth); |
| 41 | +} |
| 42 | + |
| 43 | +void Jsonnet::SetStringOutput(bool string_output) { |
| 44 | + ::jsonnet_string_output(vm_, string_output); |
| 45 | +} |
| 46 | + |
| 47 | +void Jsonnet::SetDebugAst(bool debug_ast) { |
| 48 | + ::jsonnet_debug_ast(vm_, static_cast<int>(debug_ast)); |
| 49 | +} |
| 50 | + |
| 51 | +void Jsonnet::SetMaxTrace(uint32_t lines) { |
| 52 | + ::jsonnet_max_trace(vm_, static_cast<unsigned>(lines)); |
| 53 | +} |
| 54 | + |
| 55 | +void Jsonnet::BindExtVar(const std::string& key, const std::string& value) { |
| 56 | + ::jsonnet_ext_var(vm_, key.c_str(), value.c_str()); |
| 57 | +} |
| 58 | + |
| 59 | +void Jsonnet::BindExtCodeVar(const std::string& key, |
| 60 | + const std::string& value) { |
| 61 | + ::jsonnet_ext_code(vm_, key.c_str(), value.c_str()); |
| 62 | +} |
| 63 | + |
| 64 | +bool Jsonnet::EvaluateFile(const std::string& filename, std::string* output) { |
| 65 | + if (output == nullptr) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + int error = 0; |
| 69 | + const char* jsonnet_output = |
| 70 | + ::jsonnet_evaluate_file(vm_, filename.c_str(), &error); |
| 71 | + output->assign(jsonnet_output); |
| 72 | + return error == 0; |
| 73 | +} |
| 74 | + |
| 75 | +bool Jsonnet::EvaluateSnippet(const std::string& filename, |
| 76 | + const std::string& snippet, |
| 77 | + std::string* output) { |
| 78 | + if (output == nullptr) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + int error = 0; |
| 82 | + const char* jsonnet_output = ::jsonnet_evaluate_snippet( |
| 83 | + vm_, filename.c_str(), snippet.c_str(), &error); |
| 84 | + output->assign(jsonnet_output); |
| 85 | + return error == 0; |
| 86 | +} |
| 87 | + |
| 88 | +namespace { |
| 89 | +void ParseMultiOutput(const char* jsonnet_output, |
| 90 | + std::map<std::string, std::string>* outputs) { |
| 91 | + for (const char* c = jsonnet_output; *c != '\0'; ) { |
| 92 | + const char *filename = c; |
| 93 | + const char *c2 = c; |
| 94 | + while (*c2 != '\0') ++c2; |
| 95 | + ++c2; |
| 96 | + const char *json = c2; |
| 97 | + while (*c2 != '\0') ++c2; |
| 98 | + ++c2; |
| 99 | + c = c2; |
| 100 | + outputs->insert(std::make_pair(filename, json)); |
| 101 | + } |
| 102 | +} |
| 103 | +} // namespace |
| 104 | + |
| 105 | +bool Jsonnet::EvaluateFileMulti( |
| 106 | + const std::string& filename, |
| 107 | + std::map<std::string, std::string>* outputs) { |
| 108 | + if (outputs == nullptr) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + int error = 0; |
| 112 | + const char* jsonnet_output = |
| 113 | + ::jsonnet_evaluate_file_multi(vm_, filename.c_str(), &error); |
| 114 | + ParseMultiOutput(jsonnet_output, outputs); |
| 115 | + return error == 0; |
| 116 | +} |
| 117 | + |
| 118 | +bool Jsonnet::EvaluateSnippetMulti( |
| 119 | + const std::string& filename, |
| 120 | + const std::string& snippet, |
| 121 | + std::map<std::string, std::string>* outputs) { |
| 122 | + if (outputs == nullptr) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + int error = 0; |
| 126 | + const char* jsonnet_output = ::jsonnet_evaluate_snippet_multi( |
| 127 | + vm_, filename.c_str(), snippet.c_str(), &error); |
| 128 | + ParseMultiOutput(jsonnet_output, outputs); |
| 129 | + return error == 0; |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace jsonnet |
0 commit comments