1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef CPP_JSONNET_H_
#define CPP_JSONNET_H_
#include <cstdint>
#include <cstring>
#include <functional>
#include <map>
#include <string>
extern "C" {
#include "libjsonnet.h"
}
namespace jsonnet {
class Jsonnet {
public:
Jsonnet();
~Jsonnet();
/// Return the version string of the Jsonnet interpreter. Conforms to
/// semantic versioning https://semver.org/. If this does not match
/// LIB_JSONNET_VERSION then there is a mismatch between header and compiled
/// library.
static std::string version();
/// Initializes the Jsonnet VM. This method must be called before calling any
/// of the other methods.
///
/// @return true if the Jsonnet VM was successfully initialized, false
/// otherwise.
bool init();
/// Sets the maximum stack depth.
void setMaxStack(uint32_t depth);
/// Sets the number of objects required before a garbage collection cycle is
/// allowed.
void setGcMinObjects(uint32_t objects);
/// Run the garbage collector after this amount of growth in the number of
/// objects.
void setGcGrowthTrigger(double growth);
/// Set whether to expect a string as output and don't JSON encode it.
void setStringOutput(bool string_output);
/// Set the number of lines of stack trace to display (0 to display all).
void setMaxTrace(uint32_t lines);
/// Add to the default import callback's library search path.
void addImportPath(const std::string& path);
/// Bind a string top-level argument for a top-level parameter.
///
/// Argument values are copied so memory should be managed by caller.
void bindTlaVar(const std::string& key, const std::string& value);
/// Bind a code top-level argument for a top-level parameter.
///
/// Argument values are copied so memory should be managed by caller.
void bindTlaCodeVar(const std::string& key, const std::string& value);
/// Bind a Jsonnet external variable to the given value.
///
/// Argument values are copied so memory should be managed by caller.
void bindExtVar(const std::string& key, const std::string& value);
/// Bind a Jsonnet external code variable to the given value.
///
/// Argument values are copied so memory should be managed by caller.
void bindExtCodeVar(const std::string& key, const std::string& value);
/// Evaluate a file containing Jsonnet code to return a JSON string.
///
/// This method returns true if the Jsonnet code is successfully evaluated.
/// Otherwise, it returns false, and the error output can be returned by
/// calling LastError();
///
/// @param filename Path to a file containing Jsonnet code.
/// @param output Pointer to string to contain the output.
/// @return true if the Jsonnet code was successfully evaluated, false
/// otherwise.
bool evaluateFile(const std::string& filename, std::string* output);
/// Evaluate a string containing Jsonnet code to return a JSON string.
///
/// This method returns true if the Jsonnet code is successfully evaluated.
/// Otherwise, it returns false, and the error output can be returned by
/// calling LastError();
///
/// @param filename Path to a file (used in error message).
/// @param snippet Jsonnet code to execute.
/// @param output Pointer to string to contain the output.
/// @return true if the Jsonnet code was successfully evaluated, false
/// otherwise.
bool evaluateSnippet(const std::string& filename, const std::string& snippet,
std::string* output);
/// Evaluate a file containing Jsonnet code, return a number of JSON files.
///
/// This method returns true if the Jsonnet code is successfully evaluated.
/// Otherwise, it returns false, and the error output can be returned by
/// calling LastError();
///
/// @param filename Path to a file containing Jsonnet code.
/// @param outputs Pointer to map which will store the output map of filename
/// to JSON string.
bool evaluateFileMulti(const std::string& filename,
std::map<std::string, std::string>* outputs);
/// Evaluate a string containing Jsonnet code, return a number of JSON files.
///
/// This method returns true if the Jsonnet code is successfully evaluated.
/// Otherwise, it returns false, and the error output can be returned by
/// calling LastError();
///
/// @param filename Path to a file containing Jsonnet code.
/// @param snippet Jsonnet code to execute.
/// @param outputs Pointer to map which will store the output map of filename
/// to JSON string.
/// @return true if the Jsonnet code was successfully evaluated, false
/// otherwise.
bool evaluateSnippetMulti(const std::string& filename, const std::string& snippet,
std::map<std::string, std::string>* outputs);
/// Returns the last error raised by Jsonnet.
std::string lastError() const;
private:
struct JsonnetVm* vm_;
std::string last_error_;
};
} // namespace jsonnet
#endif // CPP_JSONNET_H_
|