| /*****************************************************************************\ | |
| * serializer.h - serializer plugin interface | |
| ***************************************************************************** | |
| * Copyright (C) SchedMD LLC. | |
| * | |
| * This file is part of Slurm, a resource management program. | |
| * For details, see <https://slurm.schedmd.com/>. | |
| * Please also read the included file: DISCLAIMER. | |
| * | |
| * Slurm is free software; you can redistribute it and/or modify it under | |
| * the terms of the GNU General Public License as published by the Free | |
| * Software Foundation; either version 2 of the License, or (at your option) | |
| * any later version. | |
| * | |
| * In addition, as a special exception, the copyright holders give permission | |
| * to link the code of portions of this program with the OpenSSL library under | |
| * certain conditions as described in each individual source file, and | |
| * distribute linked combinations including the two. You must obey the GNU | |
| * General Public License in all respects for all of the code used other than | |
| * OpenSSL. If you modify file(s) with this exception, you may extend this | |
| * exception to your version of the file(s), but you are not obligated to do | |
| * so. If you do not wish to do so, delete this exception statement from your | |
| * version. If you delete this exception statement from all source files in | |
| * the program, then also delete it here. | |
| * | |
| * Slurm is distributed in the hope that it will be useful, but WITHOUT ANY | |
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
| * details. | |
| * | |
| * You should have received a copy of the GNU General Public License along | |
| * with Slurm; if not, write to the Free Software Foundation, Inc., | |
| * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
| \*****************************************************************************/ | |
| #ifndef _INTERFACES_SERIALIZER_H | |
| #define _INTERFACES_SERIALIZER_H | |
| #include "src/common/data.h" | |
| #include "src/common/macros.h" | |
| #include "src/common/pack.h" | |
| #include "src/interfaces/data_parser.h" | |
| typedef enum { | |
| SER_FLAGS_NONE = 0, /* defaults to compact currently */ | |
| SER_FLAGS_COMPACT = SLURM_BIT(0), | |
| SER_FLAGS_PRETTY = SLURM_BIT(1), | |
| SER_FLAGS_COMPLEX = SLURM_BIT(2), /* Dump Infinity and NaN */ | |
| SER_FLAGS_NO_TAG = SLURM_BIT(3), /* don't dump YAML tags */ | |
| SER_FLAGS_JSON6 = SLURM_BIT(4), | |
| /* Flags selecting an output layout. At most one may ever be set. */ | |
| SER_FLAGS_LAYOUT_MASK = (SER_FLAGS_COMPACT | SER_FLAGS_PRETTY), | |
| } serializer_flags_t; | |
| typedef enum { | |
| SER_MIMETYPE_FLAGS_NONE = 0, | |
| SER_MIMETYPE_FLAGS_OPENAPI = SLURM_BIT(0), | |
| } serializer_mimetype_flags_t; | |
| /* Plugin MIME type arrays end with an entry whose mimetype is NULL. */ | |
| typedef struct { | |
| serializer_mimetype_flags_t flags; | |
| char *mimetype; | |
| } serializer_mimetype_t; | |
| /* | |
| * Define common MIME types to make it easier for serializer callers. | |
| * | |
| * WARNING: There is no guarantee that plugins for these types | |
| * will be loaded at any given time. | |
| * | |
| * Each must be the primary type of the plugin providing it: the primary is | |
| * what get_mime_type_array(false) returns and serializer_g_init() matches | |
| * to hand a plugin its configuration. Compatibility aliases stay registered | |
| * for inbound requests but must not be named here. | |
| */ | |
| #define MIME_TYPE_YAML "application/yaml" /* RFC9512 */ | |
| #define MIME_TYPE_YAML_PLUGIN "serializer/yaml" | |
| #define ENV_CONFIG_YAML "SLURM_YAML" | |
| #define MIME_TYPE_JSON "application/json" | |
| #define MIME_TYPE_JSON_PLUGIN "serializer/json" | |
| #define ENV_CONFIG_JSON "SLURM_JSON" | |
| #define MIME_TYPE_URL_ENCODED "application/x-www-form-urlencoded" | |
| #define MIME_TYPE_URL_ENCODED_PLUGIN "serializer/url-encoded" | |
| #define MIME_TYPE_XJSON_PLUGIN "serializer/xjson" | |
| #define MIME_TYPE_JSON_PATCH "application/json-patch+json" /* RFC6902 */ | |
| #define MIME_TYPE_JSON_PATCH_PLUGIN "serializer/json-patch" | |
| /* | |
| * Serialize data in src into string dest | |
| * IN/OUT dest - ptr to NULL string ptr to set with output data. | |
| * caller must xfree(dest) if set. Pointer is not changed on failure. | |
| * IN/OUT length - set with number of bytes written to *dest. | |
| * Note: length will not include '\0' | |
| * IN src - populated data ptr to serialize | |
| * IN mime_type - serialize data into the given mime_type | |
| * IN flags - optional flags to specify to serilzier to change presentation of | |
| * data | |
| * RET SLURM_SUCCESS or error | |
| */ | |
| extern int serialize_g_data_to_string(char **dest, size_t *length, data_t *src, | |
| const char *mime_type, | |
| serializer_flags_t flags); | |
| /* | |
| * serialize string in src into data dest | |
| * IN/OUT dest - ptr to NULL data ptr to set with output data. | |
| * caller must FREE_NULL_DATA(dest) if set. | |
| * serializer/json-patch instead takes the target document in *dest and | |
| * patches it in place, leaving it unchanged on failure. | |
| * IN src - string to deserialize | |
| * IN length - number of bytes in src | |
| * IN mime_type - deserialize data using given mime_type | |
| * RET SLURM_SUCCESS or error | |
| * SLURM_NO_CHANGE_IN_DATA - serializer/json-patch only: a nonempty patch | |
| * leaves the document unchanged | |
| */ | |
| extern int serialize_g_string_to_data(data_t **dest, const char *src, | |
| size_t length, const char *mime_type); | |
| typedef struct serialize_parse_state_s serialize_parse_state_t; | |
| /* | |
| * Parse given (potentially partial) string buffer into target struct dst | |
| * IN/OUT state_ptr - Pointer populated with parsing state | |
| * Only populated when src ran out before the parse completed, marking | |
| * where the parse stopped. Supply the rest of src and call again with the | |
| * same state_ptr to resume. Released and set to NULL once the parse | |
| * completes or fails. | |
| * Passing a NULL src will always release state_ptr. | |
| * IN parser - return from data_parser_g_new() | |
| * IN type - expected data_parser type of obj | |
| * IN dst - ptr to struct/scalar to populate | |
| * This *must* be a pointer to the object and not just a value of the | |
| * object. | |
| * IN dst_bytes - size of object pointed to by dst | |
| * IN src - string buffer to parse into obj. | |
| * [offset, size) will be string to be read. | |
| * src offset will be set with how many bytes have been processed | |
| * IN mime_type - deserialize data using given mime_type | |
| * RET SLURM_SUCCESS once the parse is complete or stopped waiting on more src | |
| * to be supplied (state_ptr populated), or error | |
| */ | |
| extern int serialize_g_parse(serialize_parse_state_t **state_ptr, | |
| data_parser_t *parser, data_parser_type_t type, | |
| void *dst, ssize_t dst_bytes, buf_t *src, | |
| const char *mime_type); | |
| typedef struct serialize_dump_state_s serialize_dump_state_t; | |
| /* | |
| * Dump given target struct src into fixed size buffer | |
| * IN/OUT state_ptr - Pointer populated with dumping state | |
| * Only populated when ENOSPC is returned, marking where the dump stopped. | |
| * Grow dst and call again with the same state_ptr to resume. Released and | |
| * set to NULL on every other return. | |
| * Passing a NULL dst will always release state_ptr. | |
| * IN parser - return from data_parser_g_new() | |
| * IN type - data_parser type of obj | |
| * IN src - ptr to struct/scalar to dump to data_t | |
| * This *must* be a pointer to the object and not just a value of the object. | |
| * IN src_bytes - size of object pointed to by src | |
| * IN/OUT dst - buffer to populate with serialized output. | |
| * Writes will try to honor the size of the buffer (but may resize it). | |
| * Offset to indicate the bytes populated. | |
| * IN flags - optional flags to specify to serilzier to change presentation of | |
| * data | |
| * RET SLURM_SUCCESS once the dump is complete, ENOSPC if dst was too small to | |
| * hold the rest of the dump and it must be resumed, or error | |
| */ | |
| extern int serialize_g_dump(serialize_dump_state_t **state_ptr, | |
| data_parser_t *parser, data_parser_type_t type, | |
| void *src, ssize_t src_bytes, buf_t *dst, | |
| const char *mime_type, serializer_flags_t flags); | |
| /* | |
| * Check if there is a plugin loaded that can handle the requested mime type | |
| * RET ptr to best matching mime type or NULL if none can match | |
| */ | |
| extern const char *resolve_mime_type(const char *mime_type, | |
| const char **plugin_ptr); | |
| /* | |
| * Provide a NULL terminated array of registered MIME types. | |
| * IN openapi - only return types flagged SER_MIMETYPE_FLAGS_OPENAPI when true; | |
| * otherwise return the primary MIME type per plugin. | |
| * RET borrowed array, valid until serializer_g_fini() | |
| * WARNING: serializer_g_init() should not be called after this is called | |
| */ | |
| extern const char **get_mime_type_array(bool openapi); | |
| /* | |
| * Ensure a plugin is loaded that can handle mime_type. | |
| */ | |
| extern void serializer_required(const char *mime_type); | |
| /* | |
| * Load and initialize serializer plugins | |
| * | |
| * IN config - string with configuration to parse or NULL | |
| * RET SLURM_SUCCESS or error | |
| */ | |
| extern int serializer_g_init(void); | |
| /* | |
| * Unload all serializer plugins | |
| */ | |
| extern void serializer_g_fini(void); | |
| #endif |