Skip to content

Commit

Permalink
refactor(NLVObject): provide a reusable method for typed params
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Oct 10, 2016
1 parent 74dc618 commit 1a30dea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
30 changes: 1 addition & 29 deletions src/domain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -907,35 +907,7 @@ NAN_METHOD(Domain::GetAllDomainStats)
v8::Local<v8::Object> recordObject = Nan::New<v8::Object>();
for (int i = 0; i < record->nparams; i++) {
virTypedParameterPtr param = record->params + i;

v8::Local<v8::Value> value;
switch(param->type) {
case VIR_TYPED_PARAM_INT:
value = Nan::New<v8::Integer>(param->value.i);
break;
case VIR_TYPED_PARAM_UINT:
value = Nan::New<v8::Integer>(param->value.ui);
break;
case VIR_TYPED_PARAM_LLONG:
value = Nan::New<v8::Number>(param->value.l);
break;
case VIR_TYPED_PARAM_ULLONG:
value = Nan::New<v8::Number>(param->value.ul);
break;
case VIR_TYPED_PARAM_DOUBLE:
value = Nan::New<v8::Number>(param->value.d);
break;
case VIR_TYPED_PARAM_BOOLEAN:
value = Nan::New<v8::Boolean>(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));
Expand Down
34 changes: 34 additions & 0 deletions src/nlv_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,40 @@ namespace NLV {
worker->SaveToPersistent("parent", parent);
Nan::AsyncQueueWorker(worker);
}

NAN_INLINE v8::Local<v8::Value> extractTypedParameter(virTypedParameterPtr param) {
Nan::EscapableHandleScope scope;

v8::Local<v8::Value> value;
switch(param->type) {
case VIR_TYPED_PARAM_INT:
value = Nan::New<v8::Integer>(param->value.i);
break;
case VIR_TYPED_PARAM_UINT:
value = Nan::New<v8::Integer>(param->value.ui);
break;
case VIR_TYPED_PARAM_LLONG:
value = Nan::New<v8::Number>(param->value.l);
break;
case VIR_TYPED_PARAM_ULLONG:
value = Nan::New<v8::Number>(param->value.ul);
break;
case VIR_TYPED_PARAM_DOUBLE:
value = Nan::New<v8::Number>(param->value.d);
break;
case VIR_TYPED_PARAM_BOOLEAN:
value = Nan::New<v8::Boolean>(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

0 comments on commit 1a30dea

Please sign in to comment.