Skip to content
Snippets Groups Projects
Select Git revision
  • 56b30cd912fd70f76baa0c20e4cf2373e4ae1745
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

map_params.go

Blame
  • map_params.go 1.51 KiB
    package struct_utils
    
    import (
    	"encoding/json"
    	"fmt"
    	"reflect"
    	"strings"
    )
    
    //convert fields in a struct to a map of parameters, as if defined in a URL
    //we use this mainly for legacy functions that expect params to be defined in a map[string]string
    //to convert the new params struct into such a map
    func MapParams(data interface{}) map[string]string {
    	params := map[string]string{}
    	addStructParams(params, reflect.ValueOf(data))
    	return params
    }
    
    //recursive function
    func addStructParams(params map[string]string, structValue reflect.Value) {
    	t := structValue.Type()
    	if t.Kind() != reflect.Struct {
    		return
    	}
    
    	for i := 0; i < t.NumField(); i++ {
    		tf := t.Field(i)
    		//recurse for embedded structs
    		if tf.Anonymous {
    			addStructParams(params, structValue.Field(i))
    		} else {
    			jsonTags := strings.Split(t.Field(i).Tag.Get("json"), ",")
    			skip := false
    			if jsonTags[0] == "-" || jsonTags[0] == "" {
    				skip = true
    			} else {
    				for _, option := range jsonTags[1:] {
    					if option == "omitempty" && structValue.Field(i).IsZero() { // ignore the field if omitempty is applicable
    						skip = true
    					}
    				}
    			}
    			if !skip {
    				//lists must be written as JSON lists so they can be unmarshalled
    				//jsut because that is how the legacy code did it
    				if t.Field(i).Type.Kind() == reflect.Slice {
    					jsonValue, _ := json.Marshal(structValue.Field(i).Interface())
    					params[jsonTags[0]] = string(jsonValue)
    				} else {
    					params[jsonTags[0]] = fmt.Sprintf("%v", structValue.Field(i).Interface())
    				}
    			}
    		}
    	}
    }