Menu

#342 fShadowEnvironment does not allow for optional variables (which can be empty)

v1.1.3
open
properties (2)
5
2017-06-21
2017-06-20
No

Unfortunately fShadowEnvironment doesn't go nice together with fAllowEmptyVars.
Here is the case. My config file contains the line:

appender.applog.File=${ProgramData}\${logname}-${USERNAME}${optionalSuffix}.log

So I have both Environment Variables and my custom variables which I set in the code.

  • ${OptionalSuffix} is added only if app is run from command line, otherwise I would like to have it empy.
  • ${UserName} I set manually in the code only when app is run as a services.
  • ${logname} is my log name set in the code
  • ${ProgramData} is an environment variable I don't touch in the code

I have 3 options:

  1. use fShadowEnvironment

    Then I will have correctly resolved all variables but when ${optionalSuffix} is empty then I will end up with the log name: C:\ProgramData\MyLogName-janekvogel${optionalSuffix}.log

  2. fAllowEmptyVars
    It will try to read every thing from System (will skip all custom variables).

  3. fShadowEnvironment | fAllowEmptyVars
    Which also doesn't work. As it will never read system variables.

I think the best approach would be just to remove the not resolved property. So in fShadowEnvironment ${optionalSuffix} should be replaced with empty string if optionalSuffix is found neither in properties nor in system variables.

            else
               // Nothing has been subtituted, just move beyond the
                // unexpanded variable.
               // TODO: replace unresolved property with empty string
               i = var_end + DELIM_STOP_LEN;

       if (shadow_env)
                replacement = props.getProperty (key);
            if (! shadow_env || (! empty_vars && replacement.empty ()))
                internal::get_env_var (replacement, key);

            if (empty_vars || ! replacement.empty ())
            {
                // Substitute the variable with its value in place.
                pattern.replace (var_start, var_end - var_start + DELIM_STOP_LEN,
                    replacement);
                changed = true;
                if (rec_exp)
                    // Retry expansion on the same spot.
                    continue;
                else
                    // Move beyond the just substituted part.
                    i = var_start + replacement.size ();
            }
            else
               // Nothing has been subtituted, just move beyond the
                // unexpanded variable.
                i = var_end + DELIM_STOP_LEN;

Discussion

  • Jan Vogelgesang

    Jan Vogelgesang - 2017-06-21

    I think the code should be changed to something like:

                bool propertyExist = props.exists(key);
                if (propertyExist)
                {
                    replacement = props.getProperty(key);
                }
    
                // if shadowing is not allowed or a property was not registered then try to look up in envrionment variables
                if (!shadow_env || !propertyExist)  
                {
                    internal::get_env_var(replacement, key); 
                }
    
                // if empty values allowed or value is not empty then perform remplacement 
                if (empty_vars || !replacement.empty ())
                {
                    ....
    
     

    Last edit: Václav Haisman 2017-06-21
  • Václav Haisman

    Václav Haisman - 2017-06-21
    • labels: --> properties
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,31 +1,38 @@
    -Unfortunately fShadowEnvironment doesn't go nice together with fAllowEmptyVars.
    +Unfortunately `fShadowEnvironment` doesn't go nice together with `fAllowEmptyVars`.
     Here is the case. My config file contains the line:
    +
    +```properties
     appender.applog.File=${ProgramData}\${logname}-${USERNAME}${optionalSuffix}.log
    +```
    +
     So I have both Environment Variables and my custom variables which I set in the code.
    -${OptionalSuffix} is added only if app is run from command line, otherwise I would like to have it empy. 
    -${UserName} I set manually in the code only when app is run as a services.
    -${logname} is my log name set in the code
    -${ProgramData} is an environment variable I don't touch in the code
    +
    +- `${OptionalSuffix}` is added only if app is run from command line, otherwise I would like to have it empy. 
    +- `${UserName}` I set manually in the code only when app is run as a services.
    +- `${logname}` is my log name set in the code
    +- `${ProgramData}` is an environment variable I don't touch in the code
    +
     I have 3 options:
    -a) use fShadowEnvironment
    -Then I will have correctly resolved all variables but when ${optionalSuffix} is empty then I will end up with the log name:
    -C:\ProgramData\MyLogName-janekvogel${optionalSuffix}.log
    
    -b) fAllowEmptyVars:
    -It will try to read every thing from System (will skip all custom variables).
    +1. use `fShadowEnvironment`
    
    -c) fShadowEnvironment | fAllowEmptyVars
    -Which also doesn't work. As it will never read system variables.
    +    Then I will have correctly resolved all variables but when ${optionalSuffix} is empty then I will end up with the log name: `C:\ProgramData\MyLogName-janekvogel${optionalSuffix}.log`
    
    -I think the best approach would be just to remove the not resolved property. So in fShadowEnvironment ${optionalSuffix} should be replaced with empty string if optionalSuffix is not found neither in properties nor in system variables.
    +2. `fAllowEmptyVars`
    +    It will try to read every thing from System (will skip all custom variables).
    
    +3. `fShadowEnvironment | fAllowEmptyVars`
    +    Which also doesn't work. As it will never read system variables.
    +
    +I think the best approach would be just to remove the not resolved property. So in `fShadowEnvironment` `${optionalSuffix}` should be replaced with empty string if optionalSuffix is found neither in properties nor in system variables.
    +
    +```cpp
                 else
                    // Nothing has been subtituted, just move beyond the
                     // unexpanded variable.
                    // TODO: replace unresolved property with empty string
                    i = var_end + DELIM_STOP_LEN;
    
    -~~~
            if (shadow_env)
                     replacement = props.getProperty (key);
                 if (! shadow_env || (! empty_vars && replacement.empty ()))
    @@ -48,4 +55,4 @@
                    // Nothing has been subtituted, just move beyond the
                     // unexpanded variable.
                     i = var_end + DELIM_STOP_LEN;
    -~~~
    +```
    
    • assigned_to: Václav Haisman
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.