github.com/janelia-flyem/dvid@v1.0.0/datatype/neuronjson/fields.go (about)

     1  package neuronjson
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  )
     7  
     8  type Fields uint8
     9  
    10  const (
    11  	ShowBasic Fields = iota
    12  	ShowUsers
    13  	ShowTime
    14  	ShowAll
    15  )
    16  
    17  func (f Fields) Bools() (showUser, showTime bool) {
    18  	switch f {
    19  	case ShowBasic:
    20  		return false, false
    21  	case ShowUsers:
    22  		return true, false
    23  	case ShowTime:
    24  		return false, true
    25  	case ShowAll:
    26  		return true, true
    27  	default:
    28  		return false, false
    29  	}
    30  }
    31  
    32  // parse query string "show" parameter into a Fields value.
    33  func showFields(r *http.Request) Fields {
    34  	switch r.URL.Query().Get("show") {
    35  	case "user":
    36  		return ShowUsers
    37  	case "time":
    38  		return ShowTime
    39  	case "all":
    40  		return ShowAll
    41  	default:
    42  		return ShowBasic
    43  	}
    44  }
    45  
    46  // parse query string "fields" parameter into a list of field names
    47  func fieldList(r *http.Request) (fields []string) {
    48  	fieldsString := r.URL.Query().Get("fields")
    49  	if fieldsString != "" {
    50  		fields = strings.Split(fieldsString, ",")
    51  	}
    52  	return
    53  }
    54  
    55  // get a map of fields (none if all) from query string "fields"
    56  func fieldMap(r *http.Request) (fields map[string]struct{}) {
    57  	fields = make(map[string]struct{})
    58  	for _, field := range fieldList(r) {
    59  		fields[field] = struct{}{}
    60  	}
    61  	return
    62  }
    63  
    64  // Remove any fields that have underscore prefix.
    65  func removeReservedFields(data NeuronJSON, showFields Fields) NeuronJSON {
    66  	var showUser, showTime bool
    67  	switch showFields {
    68  	case ShowBasic:
    69  		// don't show either user or time -- default values
    70  	case ShowUsers:
    71  		showUser = true
    72  	case ShowTime:
    73  		showTime = true
    74  	case ShowAll:
    75  		return data
    76  	}
    77  	out := data.copy()
    78  	for field := range data {
    79  		if (!showUser && strings.HasSuffix(field, "_user")) || (!showTime && strings.HasSuffix(field, "_time")) {
    80  			delete(out, field)
    81  		}
    82  	}
    83  	return out
    84  }
    85  
    86  // Return a subset of fields where
    87  //
    88  //	onlyFields is a map of field names to include
    89  //	hideSuffixes is a map of fields suffixes (e.g., "_user") to exclude
    90  func selectFields(data NeuronJSON, fieldMap map[string]struct{}, showUser, showTime bool) NeuronJSON {
    91  	out := data.copy()
    92  	if len(fieldMap) > 0 {
    93  		for field := range data {
    94  			if field == "bodyid" {
    95  				continue
    96  			}
    97  			if _, found := fieldMap[field]; found {
    98  				if !showUser {
    99  					delete(out, field+"_user")
   100  				}
   101  				if !showTime {
   102  					delete(out, field+"_time")
   103  				}
   104  			} else {
   105  				delete(out, field)
   106  				delete(out, field+"_time")
   107  				delete(out, field+"_user")
   108  			}
   109  		}
   110  	} else {
   111  		if !showUser {
   112  			for field := range data {
   113  				if strings.HasSuffix(field, "_user") {
   114  					delete(out, field)
   115  				}
   116  			}
   117  		}
   118  		if !showTime {
   119  			for field := range data {
   120  				if strings.HasSuffix(field, "_time") {
   121  					delete(out, field)
   122  				}
   123  			}
   124  		}
   125  	}
   126  	return out
   127  }