0

I got a .ini configuration file that I want to use to initialize a Configuration struct.

I'd like to use the Configuration fields names and loop over them to populate my new instance with the corresponding value in the .ini file.

I thought the best way to achieve this might be reflection API (maybe I'm totally wrong, tell me...)

My problem here is that I cannot figure out how to access field's name (if it is at least possible)

Here is my code:

package test
import(
     "reflect"
     "gopkg.in/ini.v1"
 )

type Config struct {
    certPath string
    keyPath  string
    caPath   string
}

func InitConfig(iniConf *ini.File) *Config{
    config:=new(Config)
    var valuePtr reflect.Value = reflect.ValueOf(config)
    var value reflect.Value = valuePtr.Elem()
    for i := 0; i < value.NumField(); i++ {
        field := value.Field(i)
        if field.Type() == reflect.TypeOf("") {
            //here is my problem, I can't get the field name, this method does not exist... :'(
            value:=cfg.GetSection("section").GetKey(field.GetName())
            field.SetString(value)
        }
    }
    return config
}

Any help appreciated...

2 Answers 2

2

Use the type to get a StructField. The StructField has the name:

 name := value.Type().Field(i).Name

Note that the ini package's File.MapTo and Section.MapTo methods implement this functionality.

Sign up to request clarification or add additional context in comments.

2 Comments

hmmm... I think I get it, thank you. Another last question, is there another way to achieve this without reflection api ?
Oh... Indeed, I didn't see that method. Thanks for the tips.
1

While @MuffinTop solved your immediate issue, I'd say you may be solving a wrong problem. I personally know of at least two packages, github.com/Thomasdezeeuw/ini and gopkg.in/gcfg.v1, which are able to parse INI-style files (of the various level of "INI-ness", FWIW) and automatically populate your struct-typed values using reflection, so for you it merely amounts to properly setting tags on the fields of your struct (if needed at all).

I used both of these packages in production so am able to immediately recommend them. You might find more packages dedicated to parsing INI files on godoc.org.

3 Comments

actualy @MuffinTop pointed this out in the comments. I was pretty sure it existed such implementations, but as a real Go-newbie, I was more curious about the underlying bare-metal way as it's a common process to assing values to dynamic Object/Struct properties. Anyway, I agree with you, it's much cleaner using libraries's built-in mapping functionality. Thanks for taking the time to answer.
@n00dl3, oh silly me: I got so fixated on your direct usage of reflection I did not spot you were already using gopkg.in/ini.v1 to parse your INI data! So yeah, the @MuffinTop's answer plus their comment is a spot-on.
JFTR: the gopkg.in/ini.v1 package wasn't among those I recommended because in my personal opinion that package is a prime example of typical overengeneering with its built-in locking for serializing R/W access being a clear Go anti-pattern. It's okay for a start though so don't bother with this for now, @n00dl3, so long it works ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.