/* Generated from test_map_string_string_array.json. Do not edit!  */

#ifndef _GNU_SOURCE
#  define _GNU_SOURCE
#endif
#include <string.h>
#include <ocispec/read-file.h>
#include "ocispec/basic_test_map_string_string_array.h"
define_cleaner_function (basic_test_map_string_string_array *, free_basic_test_map_string_string_array)

basic_test_map_string_string_array *
make_basic_test_map_string_string_array (json_object *tree, const struct parser_context *ctx, parser_error *err)
{
    __auto_cleanup (free_basic_test_map_string_string_array) basic_test_map_string_string_array *ret = NULL;
    *err = NULL;
    (void) ctx; /* Silence compiler warning.  */
    if (tree == NULL)
      return NULL;
    ret = calloc (1, sizeof (*ret));
    if (ret == NULL)
      return NULL;
  do
    {
      json_object * tmp = get_val (tree, "maps", json_type_array);
      if (tmp != NULL && json_object_is_type (tmp, json_type_array))
        {
          size_t i;
          size_t len = json_object_array_length (tmp);
          ret->maps_len = len;
      ret->maps = calloc (len + 1, sizeof (*ret->maps));
      if (ret->maps == NULL)
        return NULL;
  for (i = 0; i < len; i++)
          {
            json_object * val = json_object_array_get_idx (tmp, i);
            ret->maps[i] = make_json_map_string_string (val, ctx, err);
            if (ret->maps[i] == NULL)
              return NULL;
          }
      }
  } while (0);
  if (json_object_is_type (tree, json_type_object))
    {
      json_object *residual_obj = NULL;
      size_t unknown_count = 0;

      json_object_object_foreach (tree, key_str, key_val)
        {
          (void) key_val;
          if (key_str != NULL
              && strcmp (key_str, "maps"))
            {
              unknown_count++;
              if (ctx->options & OPT_PARSE_FULLKEY)
                {
                  if (residual_obj == NULL)
                    {
                      residual_obj = json_object_new_object ();
                      if (residual_obj == NULL)
                        return NULL;
                    }
                  json_object_object_add (residual_obj, key_str, json_object_get (key_val));
                }
            }
        }

      if ((ctx->options & OPT_PARSE_STRICT) && unknown_count > 0 && ctx->errfile != NULL)
        (void) fprintf (ctx->errfile, "WARNING: unknown key found\n");

      if ((ctx->options & OPT_PARSE_FULLKEY) && residual_obj != NULL)
        ret->_residual = residual_obj;
    }
  return move_ptr (ret);
}

void
free_basic_test_map_string_string_array (basic_test_map_string_string_array *ptr)
{
    if (ptr == NULL)
        return;
  if (ptr->maps != NULL)
    {
      size_t i;
      for (i = 0; i < ptr->maps_len; i++)
        {
          if (ptr->maps[i] != NULL)
            {
              free_json_map_string_string (ptr->maps[i]);
              ptr->maps[i] = NULL;
            }
        }
    free (ptr->maps);
    ptr->maps = NULL;
  }
  json_object_put (ptr->_residual);
  ptr->_residual = NULL;
  free (ptr);
}
json_gen_status
gen_basic_test_map_string_string_array (json_gen_ctx * g, const basic_test_map_string_string_array *ptr, const struct parser_context *ctx, parser_error *err)
{
    json_gen_status stat = json_gen_status_ok;
    *err = NULL;
    (void) ptr; /* Silence compiler warning.  */
  stat = json_gen_map_open (g);
  if (stat != json_gen_status_ok)
    GEN_SET_ERROR_AND_RETURN (stat, err);
  if ((ctx->options & OPT_GEN_KEY_VALUE) || (ptr != NULL && ptr->maps != NULL))
    {
      size_t len = 0, i;
    stat = json_gen_string (g, (const char *) ("maps"), 4 /* strlen ("maps") */);
    if (stat != json_gen_status_ok)
      GEN_SET_ERROR_AND_RETURN (stat, err);
    if (ptr != NULL && ptr->maps != NULL)
      len = ptr->maps_len;
    if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))
        json_gen_config (g, json_gen_beautify, 0);
    stat = json_gen_array_open (g);
    if (stat != json_gen_status_ok)
      GEN_SET_ERROR_AND_RETURN (stat, err);
    for (i = 0; i < len; i++)
      {
        stat = gen_json_map_string_string (g, ptr->maps[i], ctx, err);
        if (stat != json_gen_status_ok)
            GEN_SET_ERROR_AND_RETURN (stat, err);
      }
    stat = json_gen_array_close (g);
    if (!len && !(ctx->options & OPT_GEN_SIMPLIFY))
        json_gen_config (g, json_gen_beautify, 1);
  }
  if (ptr != NULL && ptr->_residual != NULL)
    {
      stat = gen_json_object_residual (ptr->_residual, g, err);
      if (json_gen_status_ok != stat)
          GEN_SET_ERROR_AND_RETURN (stat, err);
    }
  stat = json_gen_map_close (g);
  if (stat != json_gen_status_ok)
    GEN_SET_ERROR_AND_RETURN (stat, err);
  return json_gen_status_ok;
}

basic_test_map_string_string_array *
clone_basic_test_map_string_string_array (basic_test_map_string_string_array *src)
{
    __auto_cleanup (free_basic_test_map_string_string_array) basic_test_map_string_string_array *ret = NULL;

    if (src == NULL)
      return NULL;

    ret = calloc (1, sizeof (*ret));
    if (ret == NULL)
      return NULL;
  if (src->maps)
    {
      ret->maps_len = src->maps_len;
      ret->maps = calloc (src->maps_len + 1, sizeof (*ret->maps));
      if (ret->maps == NULL)
        return NULL;
      for (size_t i = 0; i < src->maps_len; i++)
        {
  if (src->maps[i] != NULL)
    {
      ret->maps[i] = clone_map_string_string (src->maps[i]);
      if (ret->maps[i] == NULL)
        return NULL;
    }
    }
    }
  return move_ptr (ret);
}

basic_test_map_string_string_array *
basic_test_map_string_string_array_parse_file (const char *filename, const struct parser_context *ctx, parser_error *err)
{
    basic_test_map_string_string_array *ptr = NULL;
    size_t filesize;
    __auto_free char *content = NULL;

    if (filename == NULL || err == NULL)
      return NULL;

    *err = NULL;
    content = read_file (filename, &filesize);
    if (content == NULL)
      {
        if (asprintf (err, "cannot read the file: %s", filename) < 0)
            *err = strdup ("error allocating memory");
        return NULL;
      }
    ptr = basic_test_map_string_string_array_parse_data (content, ctx, err);
    return ptr;
}
basic_test_map_string_string_array *
basic_test_map_string_string_array_parse_file_stream (FILE *stream, const struct parser_context *ctx, parser_error *err)
{
    basic_test_map_string_string_array *ptr = NULL;
    size_t filesize;
    __auto_free char *content = NULL;

    if (stream == NULL || err == NULL)
      return NULL;

    *err = NULL;
    content = fread_file (stream, &filesize);
    if (content == NULL)
      {
        *err = strdup ("cannot read the file");
        return NULL;
      }
    ptr = basic_test_map_string_string_array_parse_data (content, ctx, err);
    return ptr;
}
define_cleaner_function (json_object *, json_object_put)

basic_test_map_string_string_array *
basic_test_map_string_string_array_parse_data (const char *jsondata, const struct parser_context *ctx, parser_error *err)
{
    basic_test_map_string_string_array *ptr = NULL;
    __auto_cleanup (json_object_put) json_object *doc = NULL;
    json_object *tree = NULL;
    struct parser_context tmp_ctx = { 0 };

    if (jsondata == NULL || err == NULL)
      return NULL;

    *err = NULL;
    if (ctx == NULL)
     ctx = (const struct parser_context *) (&tmp_ctx);

    doc = json_tokener_parse (jsondata);
    if (doc == NULL)
      {
        *err = strdup ("cannot parse the data");
        return NULL;
      }
    tree = doc;
    if (tree == NULL)
      {
        *err = strdup ("cannot parse the data");
        return NULL;
      }
    ptr = make_basic_test_map_string_string_array (tree, ctx, err);
    return ptr;
}
static void
cleanup_json_gen_ctx (json_gen_ctx *g)
{
  if (! g)
    return;
  json_gen_free (g);
}

define_cleaner_function (json_gen_ctx *, cleanup_json_gen_ctx)

char *
basic_test_map_string_string_array_generate_json (const basic_test_map_string_string_array *ptr, const struct parser_context *ctx, parser_error *err)
{
    __auto_cleanup (cleanup_json_gen_ctx) json_gen_ctx *g = NULL;
    struct parser_context tmp_ctx = { 0 };
    const char *gen_buf = NULL;
    char *json_buf = NULL;
    size_t gen_len = 0;

    if (ptr == NULL || err == NULL)
      return NULL;

    *err = NULL;
    if (ctx == NULL)
        ctx = (const struct parser_context *) (&tmp_ctx);

    if (! json_gen_init (&g, ctx))
      {
        *err = strdup ("Json_gen init failed");
        return json_buf;
      }

    if (json_gen_status_ok != gen_basic_test_map_string_string_array (g, ptr, ctx, err))
      {
        if (*err == NULL)
            *err = strdup ("Failed to generate json");
        return json_buf;
      }

    json_gen_get_buf (g, &gen_buf, &gen_len);
    if (gen_buf == NULL)
      {
        *err = strdup ("Error to get generated json");
        return json_buf;
      }

    json_buf = calloc (1, gen_len + 1);
    if (json_buf == NULL)
      {
        *err = strdup ("Cannot allocate memory");
        return json_buf;
      }
    (void) memcpy (json_buf, gen_buf, gen_len);
    json_buf[gen_len] = '\0';

    return json_buf;
}
