| 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 |
#ifndef CPP_JSONNET_H_ |
| 18 |
#define CPP_JSONNET_H_ |
| 19 |
|
| 20 |
#include <cstdint> |
| 21 |
#include <cstring> |
| 22 |
#include <functional> |
| 23 |
#include <map> |
| 24 |
#include <string> |
| 25 |
|
| 26 |
extern "C" { |
| 27 |
#include "libjsonnet.h" |
| 28 |
} |
| 29 |
|
| 30 |
namespace jsonnet { |
| 31 |
class Jsonnet { |
| 32 |
public: |
| 33 |
Jsonnet(); |
| 34 |
~Jsonnet(); |
| 35 |
|
| 36 |
/// Return the version string of the Jsonnet interpreter. Conforms to |
| 37 |
/// semantic versioning https://semver.org/. If this does not match |
| 38 |
/// LIB_JSONNET_VERSION then there is a mismatch between header and compiled |
| 39 |
/// library. |
| 40 |
static std::string version(); |
| 41 |
|
| 42 |
/// Initializes the Jsonnet VM. This method must be called before calling any |
| 43 |
/// of the other methods. |
| 44 |
/// |
| 45 |
/// @return true if the Jsonnet VM was successfully initialized, false |
| 46 |
/// otherwise. |
| 47 |
bool init(); |
| 48 |
|
| 49 |
/// Sets the maximum stack depth. |
| 50 |
void setMaxStack(uint32_t depth); |
| 51 |
|
| 52 |
/// Sets the number of objects required before a garbage collection cycle is |
| 53 |
/// allowed. |
| 54 |
void setGcMinObjects(uint32_t objects); |
| 55 |
|
| 56 |
/// Run the garbage collector after this amount of growth in the number of |
| 57 |
/// objects. |
| 58 |
void setGcGrowthTrigger(double growth); |
| 59 |
|
| 60 |
/// Set whether to expect a string as output and don't JSON encode it. |
| 61 |
void setStringOutput(bool string_output); |
| 62 |
|
| 63 |
/// Set whether to include a trailing newline in manifested/string output. |
| 64 |
void setTrailingNewline(bool enable); |
| 65 |
|
| 66 |
/// Set the number of lines of stack trace to display (0 to display all). |
| 67 |
void setMaxTrace(uint32_t lines); |
| 68 |
|
| 69 |
/// Add to the default import callback's library search path. |
| 70 |
/// Note this is not an isolation mechanism or a way to restrict access to the file |
| 71 |
/// system. Imports can directly refer to absolute paths, or traverse 'up' the |
| 72 |
/// filesystem with '..' paths. They are not limited to paths below the specified |
| 73 |
/// search paths. |
| 74 |
void addImportPath(const std::string& path); |
| 75 |
|
| 76 |
/// Bind a string top-level argument for a top-level parameter. |
| 77 |
/// |
| 78 |
/// Argument values are copied so memory should be managed by caller. |
| 79 |
void bindTlaVar(const std::string& key, const std::string& value); |
| 80 |
|
| 81 |
/// Bind a code top-level argument for a top-level parameter. |
| 82 |
/// |
| 83 |
/// Argument values are copied so memory should be managed by caller. |
| 84 |
void bindTlaCodeVar(const std::string& key, const std::string& value); |
| 85 |
|
| 86 |
/// Bind a Jsonnet external variable to the given value. |
| 87 |
/// |
| 88 |
/// Argument values are copied so memory should be managed by caller. |
| 89 |
void bindExtVar(const std::string& key, const std::string& value); |
| 90 |
|
| 91 |
/// Bind a Jsonnet external code variable to the given value. |
| 92 |
/// |
| 93 |
/// Argument values are copied so memory should be managed by caller. |
| 94 |
void bindExtCodeVar(const std::string& key, const std::string& value); |
| 95 |
|
| 96 |
/// Evaluate a file containing Jsonnet code to return a JSON string. |
| 97 |
/// |
| 98 |
/// This method returns true if the Jsonnet code is successfully evaluated. |
| 99 |
/// Otherwise, it returns false, and the error output can be returned by |
| 100 |
/// calling LastError(); |
| 101 |
/// |
| 102 |
/// @param filename Path to a file containing Jsonnet code. |
| 103 |
/// @param output Pointer to string to contain the output. |
| 104 |
/// @return true if the Jsonnet code was successfully evaluated, false |
| 105 |
/// otherwise. |
| 106 |
bool evaluateFile(const std::string& filename, std::string* output); |
| 107 |
|
| 108 |
/// Evaluate a string containing Jsonnet code to return a JSON string. |
| 109 |
/// |
| 110 |
/// This method returns true if the Jsonnet code is successfully evaluated. |
| 111 |
/// Otherwise, it returns false, and the error output can be returned by |
| 112 |
/// calling LastError(); |
| 113 |
/// |
| 114 |
/// @param filename Path to a file (used in error message). |
| 115 |
/// @param snippet Jsonnet code to execute. |
| 116 |
/// @param output Pointer to string to contain the output. |
| 117 |
/// @return true if the Jsonnet code was successfully evaluated, false |
| 118 |
/// otherwise. |
| 119 |
bool evaluateSnippet(const std::string& filename, const std::string& snippet, |
| 120 |
std::string* output); |
| 121 |
|
| 122 |
/// Evaluate a file containing Jsonnet code, return a number of JSON files. |
| 123 |
/// |
| 124 |
/// This method returns true if the Jsonnet code is successfully evaluated. |
| 125 |
/// Otherwise, it returns false, and the error output can be returned by |
| 126 |
/// calling LastError(); |
| 127 |
/// |
| 128 |
/// @param filename Path to a file containing Jsonnet code. |
| 129 |
/// @param outputs Pointer to map which will store the output map of filename |
| 130 |
/// to JSON string. |
| 131 |
bool evaluateFileMulti(const std::string& filename, |
| 132 |
std::map<std::string, std::string>* outputs); |
| 133 |
|
| 134 |
/// Evaluate a string containing Jsonnet code, return a number of JSON files. |
| 135 |
/// |
| 136 |
/// This method returns true if the Jsonnet code is successfully evaluated. |
| 137 |
/// Otherwise, it returns false, and the error output can be returned by |
| 138 |
/// calling LastError(); |
| 139 |
/// |
| 140 |
/// @param filename Path to a file containing Jsonnet code. |
| 141 |
/// @param snippet Jsonnet code to execute. |
| 142 |
/// @param outputs Pointer to map which will store the output map of filename |
| 143 |
/// to JSON string. |
| 144 |
/// @return true if the Jsonnet code was successfully evaluated, false |
| 145 |
/// otherwise. |
| 146 |
bool evaluateSnippetMulti(const std::string& filename, const std::string& snippet, |
| 147 |
std::map<std::string, std::string>* outputs); |
| 148 |
|
| 149 |
/// Returns the last error raised by Jsonnet. |
| 150 |
std::string lastError() const; |
| 151 |
|
| 152 |
private: |
| 153 |
struct JsonnetVm* vm_; |
| 154 |
std::string last_error_; |
| 155 |
}; |
| 156 |
|
| 157 |
} // namespace jsonnet |
| 158 |
|
| 159 |
#endif // CPP_JSONNET_H_ |