From 1a30dea923e332324a3431093fa6849c00845d53 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Mon, 10 Oct 2016 19:30:26 -0400 Subject: [PATCH] refactor(NLVObject): provide a reusable method for typed params --- src/domain.cc | 30 +----------------------------- src/nlv_object.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/domain.cc b/src/domain.cc index 2efcaff..8b03c45 100644 --- a/src/domain.cc +++ b/src/domain.cc @@ -907,35 +907,7 @@ NAN_METHOD(Domain::GetAllDomainStats) v8::Local recordObject = Nan::New(); for (int i = 0; i < record->nparams; i++) { virTypedParameterPtr param = record->params + i; - - v8::Local value; - switch(param->type) { - case VIR_TYPED_PARAM_INT: - value = Nan::New(param->value.i); - break; - case VIR_TYPED_PARAM_UINT: - value = Nan::New(param->value.ui); - break; - case VIR_TYPED_PARAM_LLONG: - value = Nan::New(param->value.l); - break; - case VIR_TYPED_PARAM_ULLONG: - value = Nan::New(param->value.ul); - break; - case VIR_TYPED_PARAM_DOUBLE: - value = Nan::New(param->value.d); - break; - case VIR_TYPED_PARAM_BOOLEAN: - value = Nan::New(param->value.b); - break; - case VIR_TYPED_PARAM_STRING: - value = Nan::New(param->value.s).ToLocalChecked(); - break; - default: - value = Nan::Null(); - } - - recordObject->Set(Nan::New(param->field).ToLocalChecked(), value); + recordObject->Set(Nan::New(param->field).ToLocalChecked(), extractTypedParameter(param)); } std::string domain_name(virDomainGetName(record->dom)); diff --git a/src/nlv_object.h b/src/nlv_object.h index f29c76b..d1ec617 100644 --- a/src/nlv_object.h +++ b/src/nlv_object.h @@ -285,6 +285,40 @@ namespace NLV { worker->SaveToPersistent("parent", parent); Nan::AsyncQueueWorker(worker); } + + NAN_INLINE v8::Local extractTypedParameter(virTypedParameterPtr param) { + Nan::EscapableHandleScope scope; + + v8::Local value; + switch(param->type) { + case VIR_TYPED_PARAM_INT: + value = Nan::New(param->value.i); + break; + case VIR_TYPED_PARAM_UINT: + value = Nan::New(param->value.ui); + break; + case VIR_TYPED_PARAM_LLONG: + value = Nan::New(param->value.l); + break; + case VIR_TYPED_PARAM_ULLONG: + value = Nan::New(param->value.ul); + break; + case VIR_TYPED_PARAM_DOUBLE: + value = Nan::New(param->value.d); + break; + case VIR_TYPED_PARAM_BOOLEAN: + value = Nan::New(param->value.b); + break; + case VIR_TYPED_PARAM_STRING: + value = Nan::New(param->value.s).ToLocalChecked(); + break; + default: + value = Nan::Null(); + } + + return scope.Escape(value); + } + }; #endif // NLV_OBJECT_H