Beispiel #1
0
MojErr MojObjectTest::emptyTest(MojObject& obj)
{
    MojObject obj2;
    MojString str1;
    MojString str2;

    bool found = false;

    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());

    MojTestAssert(obj.begin() == obj.end());
    MojTestAssert(!obj.contains(_T("hello")));
    MojTestAssert(!obj.get(_T("hi"), obj2));
    MojErr err = obj.del(_T("hello"), found);
    MojTestErrCheck(err);

    obj2.assign(obj);
    MojTestAssert(obj2.type() == obj.type());
    MojTestAssert(obj2 == obj);
    MojTestAssert(!(obj2 != obj));

    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    err = obj2.stringValue(str2);
    MojTestErrCheck(err);
    MojTestAssert(str1 == str2);

    return MojErrNone;
}
Beispiel #2
0
MojErr MojDb::mergeInto(MojObject& dest, const MojObject& obj, const MojObject& prev)
{
	MojLogTrace(s_log);

	// TODO: support field deletion
	// TODO: move merge fn out of db
	MojErr err;
	MojObject::ConstIterator objIter = obj.begin();
	MojObject::ConstIterator prevIter = prev.begin();
	while (objIter != obj.end() || prevIter != prev.end()) {
		// compare keys from two iters
		int comp;
		if (objIter == obj.end()) {
			comp = 1;
		} else if (prevIter == prev.end()) {
			comp = -1;
		} else {
			comp = objIter.key().compare(prevIter.key());
		}
		// put the appropriate value into dest
		if (comp > 0) {
			err = dest.put(prevIter.key(), prevIter.value());
			MojErrCheck(err);
			++prevIter;
		} else if (comp < 0) {
			err = dest.put(objIter.key(), objIter.value());
			MojErrCheck(err);
			++objIter;
		} else {
			MojObject::Type newType = objIter.value().type();
			MojObject::Type prevType = prevIter.value().type();
			if (newType == MojObject::TypeObject && prevType == MojObject::TypeObject) {
				MojObject merged(MojObject::TypeObject);
				err = mergeInto(merged, objIter.value(), prevIter.value());
				MojErrCheck(err);
				err = dest.put(objIter.key(), merged);
				MojErrCheck(err);
			} else {
				err = dest.put(objIter.key(), objIter.value());
				MojErrCheck(err);
			}
			++prevIter;
			++objIter;
		}
	}
	return MojErrNone;
}
Beispiel #3
0
MojErr MojLogEngine::configure(const MojObject& conf, const MojChar* name)
{
	MojAssert(name);

	MojErr err = MojErrNone;
	MojLogger::Level defaultLevel = MojLogger::LevelDefault;
	MojAutoPtr<MojLogAppender> appender;
	LevelMap levels;

	// validate entire configuration before we apply any changes
	MojObject logConf;
	if (conf.get(LogKey, logConf)) {
		// appender
		MojObject obj;
		if (logConf.get(AppenderKey, obj)) {
			err = createAppender(obj, name, appender);
			MojErrCheck(err);
		}
		// levels
		if (logConf.get(LevelsKey, obj)) {
			// configure loggers
			MojString str;
			for (MojObject::ConstIterator i = obj.begin(); i != obj.end(); ++i) {
				MojLogger::Level level = MojLogger::LevelNone;
				err = i.value().stringValue(str);
				MojErrCheck(err);
				err = MojLogger::levelFromString(str, level);
				MojErrCheck(err);
				if (i.key() == DefaultKey) {
					defaultLevel = level;
				} else {
					err = levels.put(i.key(), level);
					MojErrCheck(err);
				}
			}
		}
	}

	// apply changes
	MojThreadGuard guard(m_mutex);
	// update name
	err = m_name.assign(name);
	MojErrCheck(err);
	// update appender
	updateAppender(appender);
	// update levels
	m_levels = levels;
	m_defaultLogger.level(defaultLevel);
	updateLoggers();

	return MojErrNone;
}
Beispiel #4
0
MojErr MojDbPropExtractor::valsImpl(const MojObject& obj, KeySet& valsOut, MojSize idx) const
{
    LOG_TRACE("Entering function %s", __FUNCTION__);

	MojAssert(!m_prop.empty() && idx < m_prop.size());

	MojErr err = MojErrNone;
	const MojString& propKey = m_prop[idx];
	if (propKey == WildcardKey) {
		// get all prop vals if we have a wildcard
		for (MojObject::ConstIterator i = obj.begin(); i != obj.end(); ++i) {
			err = handleVal(*i, valsOut, idx);
			MojErrCheck(err);
		}
	} else {
		// get object corresponding to the current component in the prop path
		MojObject::ConstIterator i = obj.find(propKey);
		if (i == obj.end()) {
			err = valsOut.put(m_default);
			MojErrCheck(err);
		} else {
			if (i->type() == MojObject::TypeArray) {
				// if the value is an array, act on its elements rather than the array object itself
				MojObject::ConstArrayIterator end = i->arrayEnd();
				for (MojObject::ConstArrayIterator j = i->arrayBegin(); j != end; ++j) {
					err = handleVal(*j, valsOut, idx);
					MojErrCheck(err);
				}
			} else {
				// not an array
				err = handleVal(*i, valsOut, idx);
				MojErrCheck(err);
			}
		}
	}
	return MojErrNone;
}
MojoObjectString::MojoObjectString(const MojObject& obj, const MojChar *key)
{
	MojErr err;

	MojObject::ConstIterator found = obj.find(key);

	if (found != obj.end()) {
		err = found.value().stringValue(m_str);
		if (err) {
			m_str.assign("(error retreiving value)");
		}
	} else {
		m_str.assign("(not found)");
	}
}