Problem parsing IPv4/IPv6 addresses with std.socket.parseAddress

Dsciple via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 27 07:57:26 PDT 2016


On Tuesday, 27 September 2016 at 14:39:10 UTC, Dsciple wrote:
> On Tuesday, 27 September 2016 at 14:02:25 UTC, Marc Schütz 
> wrote:
>> On Tuesday, 27 September 2016 at 09:04:53 UTC, Dsciple wrote:
>>> As said, this works fine when tested in isolation, and the 
>>> compiler only complains when using BindAddress as a member of 
>>> ConfigParams.
>>> Any idea what the problem may be?
>>> Or is there maybe a ready to use, high-level library for 
>>> parsing parameters from command-line arguments and config 
>>> files of some kind?
>>
>> I assume your ConfigParams variable is global or static? Can 
>> you show how you initialize it, and how it's declared?
>>
>> You're probably using it in a way that requires it to be 
>> evaluated at compile time. That's the case for initializers of 
>> global/static variables, as well as default values of struct 
>> members.
>
> Yes I think so.
> I use static default values for all members of ConfigParams and 
> I instantiate ConfigParams in my unit tests, so I assume that 
> the variable would be global there.
> The code looks like:
>
> unittest {
>
>   string[] args = [
>     "binaryFileName",
>     "--bindAddresses=0.1.2.3;4.5.6.7",
>     "--bindHTTPPort=80",
>     "--bindHTTPSPort=443",
>     "--configFile=testfiles/test.conf.sdl",
>     "--verbosityLevel=detailed",
>   ];
>
>   ConfigParams configParams;  // default values for parameters
>   configParams.readFromAll(args); // values read from 
> command-line arguents
>
>   // assertion checks here
> }
>
> What do you suggest? Should I move all default initializations 
> to a constructor?
> Thank you for your response.

I forgot to show declaration. Here it is:

struct ConfigParams {

   import ConfigParamsCLAMixin: ConfigParamsCLA;
   import ConfigParamsSDLMixin: ConfigParamsSDL;
   import ConfigParamsAllMixin: ConfigParamsAll;
   import BindAddress: BindAddress, BindAddressException;
   import BindAddresses: BindAddresses, BindAddressesException;
   import BindPort: BindPort, BindPortException;
   import PosixPath: PosixPath, PosixPathException;
   import VerbosityLevel: VerbosityLevel, VerbosityLevelException;

   // Define configuration parameters' static default fields
   static immutable BindAddresses defaultBindAddresses = 
BindAddresses([
     BindAddress("192.168.2.10")
   ]);
   static immutable BindPort defaultBindHTTPPort = BindPort(8080);
   static immutable BindPort defaultBindHTTPSPort = BindPort(4430);
   static immutable PosixPath defaultConfigFile =
     PosixPath("/etc/ras/ras.conf.sdl");
   static immutable VerbosityLevel defaultVerbosityLevel =
     VerbosityLevel("quiet");

   // Define configuration parameters fields with default values
   BindAddresses bindAddresses = defaultBindAddresses;
   BindPort bindHTTPPort = defaultBindHTTPPort;
   BindPort bindHTTPSPort = defaultBindHTTPSPort;
   PosixPath configFile = defaultConfigFile;
   VerbosityLevel verbosityLevel = defaultVerbosityLevel;

}

I don't understand why the unit tests of all individual members 
of ConfigParams (BindAddresses, BindAddress and so on) would work 
in isolation using the same kind of initialization, whereas some 
of them would fail as members of ConfigParams.
I suspect this may also be a problem with settings in dub.json 
for ConfigParams:

{
   ...
   "sourcePaths": [
     "sources"
   ],
   "importPaths": [
     "sources"
   ],
   "configurations": [
     {
       "name": "library",
       "targetName": "ConfigParams",
       "targetType": "library"
     }
   ],
   "dependencies": {
     "bind-address": {
       "version": "*",
       "path": "./libraries/BindAddress"
     },
     "bind-addresses": {
       "version": "*",
       "path": "./libraries/BindAddresses"
     },
     "bind-port": {
       "version": "*",
       "path": "./libraries/BindPort"
     },
     "posix-name": {
       "version": "*",
       "path": "./libraries/PosixName"
     },
     "posix-path": {
       "version": "*",
       "path": "./libraries/PosixPath"
     },
     "verbosity-level": {
       "version": "*",
       "path": "./libraries/VerbosityLevel"
     }
   }

if I include bind-address as a dependency (as above), dub refuses 
to run with:

Sub package bind-address: doesn't exist.

although the package is there and used by bind-addresses and 
confi params too...



More information about the Digitalmars-d-learn mailing list