
/**
 * Autogenerated by Thrift
 *
 * DO NOT EDIT
 *  @generated
 *
 */

#include <folly/python/import.h>
#include <thrift/lib/python/capi/iobuf.h>
#include <thrift/lib/python/types.h>

#include <thrift/compiler/test/fixtures/transitive-deps/gen-python-capi/c/thrift_types_api.h>
#include <thrift/compiler/test/fixtures/transitive-deps/gen-python-capi/c/thrift_types_capi.h>


namespace apache {
namespace thrift {
namespace python {
namespace capi {
namespace {
bool ensure_module_imported() {
  static ::folly::python::import_cache_nocapture import((
      ::import_c__thrift_types_capi));
  return import();
}
  static constexpr std::int16_t _fbthrift__C__tuple_pos[1] = {
    1
  };
} // namespace

ExtractorResult<::cpp2::C>
Extractor<::cpp2::C>::operator()(PyObject* obj) {
  int tCheckResult = typeCheck(obj);
  if (tCheckResult != 1) {
      if (tCheckResult == 0) {
        PyErr_SetString(PyExc_TypeError, "Not a C");
      }
      return extractorError<::cpp2::C>(
          "Marshal error: C");
  }
  StrongRef fbThriftData(getThriftData(obj));
  return Extractor<::apache::thrift::python::capi::ComposedStruct<
      ::cpp2::C>>{}(*fbThriftData);
}

ExtractorResult<::cpp2::C>
Extractor<::apache::thrift::python::capi::ComposedStruct<
    ::cpp2::C>>::operator()(PyObject* fbThriftData) {
  ::cpp2::C cpp;
  std::optional<std::string_view> error;
  Extractor<int64_t>{}.extractInto(
      cpp.i_ref(),
      PyTuple_GET_ITEM(fbThriftData, _fbthrift__C__tuple_pos[0]),
      error);
  if (error) {
    return folly::makeUnexpected(*error);
  }
  return cpp;
}


int Extractor<::cpp2::C>::typeCheck(PyObject* obj) {
  if (!ensure_module_imported()) {
    ::folly::python::handlePythonError(
      "Module c import error");
  }
  int result =
      can_extract__c__C(obj);
  if (result < 0) {
    ::folly::python::handlePythonError(
      "Unexpected type check error: C");
  }
  return result;
}


PyObject* Constructor<::cpp2::C>::operator()(
    const ::cpp2::C& val) {
  if (!ensure_module_imported()) {
    DCHECK(PyErr_Occurred() != nullptr);
    return nullptr;
  }
  Constructor<::apache::thrift::python::capi::ComposedStruct<
        ::cpp2::C>> ctor;
  StrongRef fbthrift_data(ctor(val));
  if (!fbthrift_data) {
    return nullptr;
  }
  return init__c__C(*fbthrift_data);
}

PyObject* Constructor<::apache::thrift::python::capi::ComposedStruct<
        ::cpp2::C>>::operator()(
    [[maybe_unused]] const ::cpp2::C& val) {
  StrongRef fbthrift_data(createStructTuple(1));
  StrongRef _fbthrift__i(
    Constructor<int64_t>{}
    .constructFrom(val.i_ref()));
  if (!_fbthrift__i ||
      setStructField(
          *fbthrift_data,
          _fbthrift__C__tuple_pos[0],
          *_fbthrift__i) == -1) {
    return nullptr;
  }
  return std::move(fbthrift_data).release();
}


ExtractorResult<::cpp2::E>
Extractor<::cpp2::E>::operator()(PyObject* obj) {
  int tCheckResult = typeCheck(obj);
  if (tCheckResult != 1) {
      if (tCheckResult == 0) {
        PyErr_SetString(PyExc_TypeError, "Not a E");
      }
      return extractorError<::cpp2::E>(
          "Marshal error: E");
  }
  return ::cpp2::E{};
}

ExtractorResult<::cpp2::E>
Extractor<::apache::thrift::python::capi::ComposedStruct<
    ::cpp2::E>>::operator()(PyObject* fbThriftData) {
  ::cpp2::E cpp;
  (void)fbThriftData;
  return cpp;
}


int Extractor<::cpp2::E>::typeCheck(PyObject* obj) {
  if (!ensure_module_imported()) {
    ::folly::python::handlePythonError(
      "Module c import error");
  }
  int result =
      can_extract__c__E(obj);
  if (result < 0) {
    ::folly::python::handlePythonError(
      "Unexpected type check error: E");
  }
  return result;
}


PyObject* Constructor<::cpp2::E>::operator()(
    const ::cpp2::E& val) {
  if (!ensure_module_imported()) {
    DCHECK(PyErr_Occurred() != nullptr);
    return nullptr;
  }
  Constructor<::apache::thrift::python::capi::ComposedStruct<
        ::cpp2::E>> ctor;
  StrongRef fbthrift_data(ctor(val));
  if (!fbthrift_data) {
    return nullptr;
  }
  return init__c__E(*fbthrift_data);
}

PyObject* Constructor<::apache::thrift::python::capi::ComposedStruct<
        ::cpp2::E>>::operator()(
    [[maybe_unused]] const ::cpp2::E& val) {
  StrongRef fbthrift_data(createStructTuple(0));
  return std::move(fbthrift_data).release();
}


} // namespace capi
} // namespace python
} // namespace thrift
} // namespace apache
