8

I really want a way to print the string representation of a field name in go. It has several use cases, but here is an example:

lets say I have a struct

type Test struct {
    Field      string `bson:"Field" json:"field"`
    OtherField int    `bson:"OtherField" json:"otherField"`
}

and, for example, I want to do a mongo find:

collection.Find(bson.M{"OtherField": someValue})

I don't like that I have to put the string "OtherField" in there. It seems brittle and easy to either misstype or have the struct change and then my query fails without me knowing it.

Is there any way to get the string "OtherField" without having to either declare a const or something like that? I know I can use reflection to a get a list of field names from a struct, but I'd really like to do something along the lines of

fieldName := nameOf(Test{}.OtherField) 
collection.Find(bson.M{fieldName: someValue})

is there any way to do this in Go?? C# 6 has the built in nameof, but digging through reflection I can't find any way to do this in Go.

Fuzzerker
  • 249
  • 1
  • 6
  • 15

2 Answers2

7

I don't really think there is. You may be able to load a set of types via reflection and generate a set of constants for the field names. So:

type Test struct {
    Field      string `bson:"Field" json:"field"`
    OtherField int    `bson:"OtherField" json:"otherField"`
}

Could generate something like:

var TestFields = struct{
      Field string
      OtherField string
}{"Field","OtherField"}

and you could use TestFields.Field as a constant.

Unfortunately, I don't know of any existing tool that does anything like that. Would be fairly simple to do, and wire up to go generate though.

EDIT:

How I'd generate it:

  1. Make a package that accepts an array of reflect.Type or interface{} and spits out a code file.
  2. Make a generate.go somewhere in my repo with main function:

    func main(){
       var text = mygenerator.Gen(Test{}, OtherStruct{}, ...)
       // write text to constants.go or something
    }
    
  3. Add //go:generate go run scripts/generate.go to my main app and run go generate
captncraig
  • 22,118
  • 17
  • 108
  • 151
  • I like this idea. any tutorials you can point me to on how to get started with a go generate? – Fuzzerker May 04 '17 at 20:05
  • Yeah, see my edit for some high level ideas of how I'd do it. That way is probably easier than trying to make a standalone tool that parses code. – captncraig May 04 '17 at 20:19
1

Here is a function that will return a []string with the struct field names. I think it comes in the order they are defined.

WARNING: Reordering the fields in the struct definition will change the order in which they appear

https://play.golang.org/p/dNATzNn47S

package main

import (
    "fmt"
    "strings"
    "regexp"
    )

type Test struct {
    Field      string `bson:"Field" json:"field"`
    OtherField int    `bson:"OtherField" json:"otherField"`
}

func main() {
    fields, err := GetFieldNames(Test{})
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(fields)
}


func GetFieldNames(i interface{}) ([]string, error) {
    // regular expression to find the unquoted json
    reg := regexp.MustCompile(`(\s*?{\s*?|\s*?,\s*?)(['"])?(?P<Field>[a-zA-Z0-9]+)(['"])?:`)

    // print struct in almost json form (fields unquoted)
    raw := fmt.Sprintf("%#v", i)

    // remove the struct name so string begins with "{"
    fjs := raw[strings.Index(raw,"{"):]

    // find and grab submatch 3
    matches := reg.FindAllStringSubmatch(fjs,-1)

    // collect
    fields := []string{}
    for _, v := range matches {
        if len(v) >= 3 && v[3] != "" {
            fields = append(fields, v[3])
        }

    }


return fields, nil

}

RayfenWindspear
  • 6,116
  • 1
  • 30
  • 42
  • 1
    But wouldn't this break things if you reorder fields at all? Is this just trading magic strings for magic numbers? – captncraig May 04 '17 at 21:02
  • @captncraig True... what you could do is create a library to create custom struct tags and just be sure to tag the structs you wanted the names for. – RayfenWindspear May 04 '17 at 21:06
  • Yeah, not even sure if order is preserved with reflection either. – captncraig May 04 '17 at 21:10
  • Updated answer doesn't use a map, so ordering should be based on the order of definition. – RayfenWindspear May 04 '17 at 21:33
  • Wow, that's kinda terrifying. If I had to choose between strings for field names and that regex, I'd duplicate strings every time. – captncraig May 04 '17 at 22:16
  • @captncraig Technically, the regex is overkill. It's the same regex used to "fix" invalid JSON with unquoted field names. I just reused it from the old `json.Unmarshal` answer. Simpler regex just to match the fields from a string like `{Field: "", Field2: 0}` would be preferred... – RayfenWindspear May 04 '17 at 22:56