Variable Name to String Name

I have a somewhat complicated project that I’m implementing a config file for. Right now, I have a huge ‘if-then’ block where the chopped up string from the file is associated with different elements of a structure, something like this:

if strings.Contains(tmpStr[0], "option1"){
     optionsStruct.Option1 = tmpStr[1]
}else if strings.Contains(tmpStr[0], "option2"){
    optionsStruct.OptionTwo = tmpStr[1]
} //so on and so forth...

I want something more succinct, more like this:

    for _, part := range splittedUpString{
         varnamestring = varNameString(part)
         optionsStruct.varnamestring = part
    } //or something like that

That way, I’m not reincarnating the ‘new averaging system’ meme. I have no idea what would be the case for “optionsStruct” not having an element that matches.

If I’m just barking up the wrong tree here, let me know. I’ve probably started down the completely wrong path in the first place.

Based on the use case as you have it displayed here, I’d do this: Go Playground - The Go Programming Language

However, this is kind of the “unstable equilibrium” solution: Any additional detail with context around what you’re doing and, especially, why you’re doing it will almost certainly change my answer!

I went with this explicit mapping because you have "option2" mapping to a field called OptionTwo. If the names of your struct fields mapped directly to the option strings, or if the fields had tags that contained the option string names, then you could use the reflect package to automatically match up everything and not have an explicit slice of setter functions. I went with a slice instead of a map because there’s no point in using a map if you have to iterate through each element to check of some string contains the key. If you told me that there are thousands or more possible keys, then I might have yet another suggestion on how to lay out the data.

Looking at the code as you have it here, I wonder what should happen when a key contains "option1option2", but maybe that never happens. Like I said: With any additional detail, I might instead suggest a completely different solution :person_shrugging: :smile:

I’m bringing this up at all because you said:

And if all you want is to map substrings to code that sets fields, then this seems like a reasonable way to do it, but if you “zoom out” and tell us why you’re doing this, then we might have a better answer. For example, if this tmpStr is parsed from JSON, then I bet this would be some struct tags and a single function call to solve!

2 Likes

In addition to @skillian’s suggestions, if the config file is, or can be made, a standard config format (like YAML, TOML, JSON, etc), you would be able to use existing packages that do the parsing for you. (Do a search on pkg.go.dev)

Also, does the option variable have to be a struct? If you use a map[string]string instead, you can effortlessly store config keys and values as they arrive from the config file reader.

I’m building a distributed video surveilance stack. There are “eyeball” processes that run on Raspberry Pis. This gets passed on, encoded as a “frame” struct using gob, to Classifiers, motion detectors and backend storage processes can either run on those Pis or other machines. I have a package that reads a common config file and does things like set the ports that the different processes listen and send on and provides the locations of classifier xml files, etc…

I remember wanting to use a map, but I don’t remember why I didn’t go with it.

Maybe time to give the map a second try… :upside_down_face:

1 Like