ResultCode parseEnumValue()

in agent/native/ext/ConfigManager.cpp [452:502]


ResultCode parseEnumValue( const OptionMetadata* optMeta, String rawValue, /* out */ ParsedOptionValue* parsedValue )
{
    ELASTIC_APM_ASSERT_VALID_PTR( optMeta );
    ELASTIC_APM_ASSERT_EQ_UINT64( optMeta->defaultValue.type, parsedOptionValueType_int );
    ELASTIC_APM_ASSERT_VALID_PTR( rawValue );
    ELASTIC_APM_ASSERT_VALID_PTR( parsedValue );
    ELASTIC_APM_ASSERT_EQ_UINT64( parsedValue->type, parsedOptionValueType_undefined );

    int foundMatch = -1;
    StringView rawValueStrView = stringToView( rawValue );

    ELASTIC_APM_FOR_EACH_INDEX( i, optMeta->additionalData.enumData.enumElementsCount )
    {
        StringView currentEnumName = stringToView( optMeta->additionalData.enumData.names[ i ] );
        if ( ! isStringViewPrefixIgnoringCase( currentEnumName, rawValueStrView ) ) continue;

        // If match is exact (i.e., not just prefix) then we return immediately
        if ( currentEnumName.length == rawValueStrView.length )
        {
            foundMatch = (int)i;
            break;
        }

        if ( optMeta->additionalData.enumData.isUniquePrefixEnough )
        {
            // If there's more than one enum name that raw value matches as a prefix
            // then it's ambiguous, and we return failure
            if ( foundMatch != -1 )
            {
                ELASTIC_APM_LOG_ERROR(
                        "Failed to parse enum configuration option - raw value matches more than one enum as a prefix."
                        " Option name: `%s'."
                        " Raw value: `%s'."
                        " At least the following enums match: `%s' and `%s'."
                        , optMeta->name
                        , rawValue
                        , optMeta->additionalData.enumData.names[ foundMatch ]
                        , optMeta->additionalData.enumData.names[ i ] );
                return resultFailure;
            }

            foundMatch = (int)i;
        }
    }

    if ( foundMatch == -1 ) return resultFailure;

    parsedValue->u.intValue = foundMatch;
    parsedValue->type = parsedOptionValueType_int;
    return resultSuccess;
}