// This is a generated file, modify: generate/templates/templates/struct_content.cc

// generated from struct_content.cc
#include <nan.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // win32

extern "C" {
  #include <git2.h>
 }

#include <iostream>
#include "../include/nodegit.h"
#include "../include/lock_master.h"
#include "../include/functions/copy.h"
#include "../include/stash_save_options.h"
#include "nodegit_wrapper.cc"

  #include "../include/str_array_converter.h"
  #include "../include/signature.h"
  #include "../include/strarray.h"
 
using namespace v8;
using namespace node;
using namespace std;

 
ConfigurableGitStashSaveOptions::ConfigurableGitStashSaveOptions(nodegit::Context *nodegitContext)
  : nodegit::ConfigurableClassWrapper<GitStashSaveOptionsTraits>(nodegitContext)
{
     git_stash_save_options  wrappedValue = GIT_STASH_SAVE_OPTIONS_INIT;
    this->raw = (git_stash_save_options*) malloc(sizeof(git_stash_save_options ));
    memcpy(this->raw, &wrappedValue, sizeof(git_stash_save_options ));
 }

ConfigurableGitStashSaveOptions::~ConfigurableGitStashSaveOptions() {
              delete this->raw->message;
          if (this->raw->paths.count) {
          for (size_t i = 0; i < this->raw->paths.count; ++i) {
            delete this->raw->paths.strings[i];
          }
          delete[] this->raw->paths.strings;
        }
   }

nodegit::ConfigurableClassWrapper<GitStashSaveOptionsTraits>::v8ConversionResult ConfigurableGitStashSaveOptions::fromJavascript(nodegit::Context *nodegitContext, v8::Local<v8::Value> input) {
  if (!input->IsObject()) {
    return {
      "Must pass object for ConfigurableGitStashSaveOptions"
    };
  }

  Nan::HandleScope scope;
  v8::Local<v8::Object> inputObj = input.As<v8::Object>();
  std::shared_ptr<ConfigurableGitStashSaveOptions> output(new ConfigurableGitStashSaveOptions(nodegitContext));

  // unpack the data into the correct fields
         {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "version");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to version"
              };
            }

            output->raw->version = static_cast<unsigned int>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "flags");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to flags"
              };
            }

            output->raw->flags = static_cast<uint32_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
             {
            v8::Local<v8::Value> maybeObject = nodegit::safeGetField(inputObj, "stasher");
            if (!maybeObject.IsEmpty() && !maybeObject->IsUndefined() && !maybeObject->IsNull()) {
              if (!maybeObject->IsObject()) {
                return {
                  "Must pass NodeGit.Signature to stasher"
                };
              }

              v8::Local<v8::Object> objectValue = maybeObject.As<v8::Object>();
              output->raw->stasher = Nan::ObjectWrap::Unwrap<GitSignature>(objectValue)->GetValue();
              output->stasher.Reset(objectValue);
            }
          }
           output->raw->message = nullptr;
        {
          v8::Local<v8::Value> maybeString = nodegit::safeGetField(inputObj, "message");
          if (!maybeString.IsEmpty() && !maybeString->IsUndefined() && !maybeString->IsNull()) {
            if (!maybeString->IsString()) {
              return {
                "Must pass string to message"
              };
            }

            Nan::Utf8String utf8String(maybeString.As<v8::String>());
            output->raw->message = strdup(*utf8String);
          }
        }
            output->raw->paths.count = 0;
          output->raw->paths.strings = nullptr;

          {
            v8::Local<v8::Value> maybeStrarray = nodegit::safeGetField(inputObj, "paths");
            if (!maybeStrarray.IsEmpty() && !maybeStrarray->IsUndefined() && !maybeStrarray->IsNull()) {
              if (maybeStrarray->IsArray()) {
                v8::Local<v8::Array> strarrayValue = maybeStrarray.As<v8::Array>();
                // validate the StrArray is indeed a list of strings
                for (uint32_t i = 0; i < strarrayValue->Length(); ++i) {
                  // TODO confirm that sparse array at least boils down to undefined
                  v8::Local<v8::Value> arrayValue = Nan::Get(strarrayValue, i).ToLocalChecked();
                  if (!arrayValue->IsString()) {
                    return {
                      "Must pass String or Array of strings to paths"
                    };
                  }
                }

                StrArrayConverter::ConvertInto(&output->raw->paths, strarrayValue);
              } else if (maybeStrarray->IsString()) {
                v8::Local<v8::String> strarrayValue = maybeStrarray.As<v8::String>();
                StrArrayConverter::ConvertInto(&output->raw->paths, strarrayValue);
              } else {
                return {
                  "Must pass String or Array of strings to paths"
                };
              }
            }
          }
     
  return {
    output
  };
}

           
// force base class template instantiation, to make sure we get all the
// methods, statics, etc.
template class nodegit::ConfigurableClassWrapper<GitStashSaveOptionsTraits>;
