count slice length in struct field - go

I think I need a better solution than my case switch as the struct gains more fields my function will become verbose. Is there a way to swap my switch for a loop?
I have the following code
type Things struct {
StreetNames []string `json:"streetNames"`
Letters []string `json:"letters"`
MaleNames []string `json:"maleNames"`
}
func CheckCategories(data *Things, filePath string) error {
errMsg := "list has no values or is a missing category in File: " + filePath
categories := []string{"street_name", "letter", "male_first_name"}
for _, value := range categories {
switch value {
case "street_name":
if len(data.StreetNames) == 0 {
return errors.New("street_name " + errMsg)
}
case "letter":
if len(data.Letters) == 0 {
return errors.New("letter " + errMsg)
}
case "male_first_name":
if len(data.MaleNames) == 0 {
return errors.New("male_first_name " + errMsg)
}
}
}
return nil
}
This works for me but the real struct contains 12 fields which makes my functions long and repetitive.
I tried
for _, value := range categories {
if len("data." + value) == 0 {
return errors.New(value + errMsg)
}
But when I ran the code I took a moment to notice it wasn't working as intended, Im getting the length of the string. I have tried data[value] but that didn't work either.

Is there a way to swap my switch for a loop?
You could do the following:
type Things struct {
StreetNames []string `json:"streetNames"`
Letters []string `json:"letters"`
MaleNames []string `json:"maleNames"`
}
func CheckCategories(data *Things, filePath string) error {
errMsg := "list has no values or is a missing category in File: " + filePath
categories := []struct{
name string
slice []string
}{
{"street_name", data.StreetNames},
{"letter", data.Letters},
{"male_first_name", data.MaleNames},
}
for _, v := range categories {
if len(v.slice) == 0 {
return errors.New(v.name + " " + errMsg)
}
}
return nil
}

Related

Keyword search with negative keywords

I have a simple question about keywords searching in a Go.
I want to search a string using positive and negative keywords
func keyword(itemTitle string, keywords string) bool {
splits := strings.Split(keywords, ",")
for _, item := range splits {
item = strings.TrimSpace(item)
fmt.Println(strings.ToUpper(itemTitle))
fmt.Println(strings.ToUpper(item))
if strings.Contains(item,"-") {
item = item[1:]
if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
return false
}
}
item = item[1:]
fmt.Println(strings.ToUpper(item))
if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
return true
}
}
return false
}
heres my searcher method
func TestKeyword(t *testing.T) {
test1 := "Pokemon Nintendo Switch Cool Thing"
keywordTest1 := "+pokemon,-nintendo"
if keyword(test1, keywordTest1) {
fmt.Println("matched")
} else {
fmt.Println("test")
}
test2 := "Pokemon Cards Cool"
if keyword(test2, keywordTest1) {
fmt.Println("matched")
} else {
fmt.Println("test")
}
}
my test cases
i understand why its not working because +amd is the first in the slice and its ofc going to return true and not test any of the other like -radeon but im just kinda stumped on what todo.
Output given
matched
matched
Expected Output
test
matched
I updated your search function but kept the signature
func keyword(itemTitle string, keywords string) bool {
a := strings.ToUpper(itemTitle)
b := strings.ToUpper(keywords)
keys := strings.Split(strings.Replace(b, "-", " ", -1), ",")
for _, key := range keys {
key = strings.TrimSpace(key)
if strings.Contains(a, key) {
return true
}
}
return false
}
And updated your test function with a passing test and a failed one to see how it works.
func TestKeyword(t *testing.T) {
test1 := "Pokemon Nintendo Switch Cool Thing"
keywordTest1 := "+pokemon,-nintendo"
if keyword(test1, keywordTest1) {
t.Log("matched")
} else {
t.Fail()
}
test2 := "Pokemon Cards Cool"
if keyword(test2, keywordTest1) {
t.Log("matched")
} else {
t.Fail()
}
}
Regarding the second test failing with a keyword with + on it, you could pass that through a regex to get only alphanumeric characters, if required.

How to do a partial update methods for row

I'm trying to write a generic function to do a partial update for row, Here is what I got so far.
func GetSetByUpdateOptions(x interface{}) (string, error) {
t := reflect.TypeOf(x)
v := reflect.ValueOf(x)
if v.Kind() != reflect.Struct {
return "", errors.New("invalid_options_tag")
}
res := "SET "
for i := 0; i < v.NumField(); i++ {
setTemplate := "%s=%v, "
field := t.Field(i).Tag.Get("sql_set")
value := v.Field(i).Interface()
// NEED TO CHECK VALUE HERE
if field == "" {
return "", errors.New("invalid_options_tag")
}
res += fmt.Sprintf(setTemplate, field, value)
}
return res, nil
}
type Optional struct {
Field1 *int `set:"field1"` // might be nil if not set
Field2 *int `set:"field2"` // might be nil if not set
}
func UpdateRow(o Optional) { // here because i want a partial update. field1
s, _ := GetSetByUpdateOptions(o)
query := "UPDATE table SET status=" + s + " mtime=UNIX_TIMESTAMP(NOW()) WHERE id = ?"
// update
}
Just wondering how to define an input type since using an interface as input for GetSetByUpdateOptions seems not a good idea and How to filter some part of options because I want a partial update?

Replace a single string in a slice for a group of strings

I want to have a slice of strings, and when certain strings are found, they are replaced by a group of related strings.
For instance, if I have this :
[]string{"A","FROM_B_TO_E","F"}
After my method runs I want to have :
[]string{"A","B","C","D","E","F"}
I came up with this code, the thing is, although I can print my to_be_added slice just before actually adding it, for some reason it does not work. It does work however if I change my translateRule so instead of returning a slice of string it only returns a single string :
func groupRules(validationRules []string){
for index,rulename := range validationRules {
if succeeded, to_be_added := translateRule(rulename) ; succeeded == true{
fmt.Println("Entro! ", to_be_added)
validationRules = append(append(validationRules[:index],to_be_added...), validationRules[index+1:]...)
}
}
}
func translateRule(rule string) ( bool , []string ) {
if rule == "rs_full" {
return true,[]string{"sapo","rana"}
}
return false,nil
}
So, my lack of Go experience or the bad code I write lead me to this :
func groupRules(validationRules []string) []string{
var tmp_slice []string
for _ ,rulename := range validationRules {
if succeeded, to_be_added := translateRule(rulename) ; succeeded == true{
tmp_slice = append(tmp_slice,to_be_added...)
}else{
tmp_slice = append(tmp_slice,rulename)
}
}
return tmp_slice
}
func translateRule(rule string) ( bool , []string ) {
if rule == "rs_full" {
return true,[]string{"sapo","rana","tigre"}
}
return false,nil
}
Now it works flawlessly.
Thank you all.

common function to create map[string]struct from slice of struct dynamically

I have two different struct as mentioned below A abd B and two process functions. Is there any way by means of which i can write a common function to generate the map[string]struct for the both the struct. Moreover, is there any way using reflection given the struct name i can create the object of the same?
type A struct {
name string
// more fields
}
type B struct {
name string
// more fields
}
func ProcessA(input []A) map[string]A {
output := make(map[string]A)
for _, v := range input {
output[v.name] = v
}
return output
}
func ProcessB(input []B) map[string]B {
output := make(map[string]B)
for _, v := range input {
output[v.name] = v
}
return output
}
Idiomatic way in Go would be to use interface.
type Named interface {
Name() string
}
type letter struct {
name string
}
func (l letter) Name() string {
return l.name
}
type A struct {
letter
// more fields
}
type B struct {
letter
// more fields
}
func ProcessNameds(input []Named) map[string]Named {
output := make(map[string]Named, len(input))
for _, v := range input {
output[v.Name()] = v
}
return output
}
Well, see if something like this would help:
package main
import (
"fmt"
"strconv"
)
type A struct {
name string
// more fields
}
type B struct {
name string
// more fields
}
func Process(x interface{}) interface{} {
ma := make(map[string]int)
mb := make(map[string]string)
if x == nil {
return nil
} else if a, ok := x.([]A); ok {
fmt.Printf("Type A argument passed %s\n", x)
ma[a[0].name] = 1
ma[a[1].name] = 2
return ma //you can return whatever type you want here
} else if b, ok := x.([]B); ok {
fmt.Printf("Type B argument passed %s\n", x)
mb[b[0].name] = "a"
mb[b[1].name] = "b"
return mb //you can return whatever type you want here
} else {
panic(fmt.Sprintf("Unexpected type %T: %v", x, x))
}
return nil
}
func main() {
a := make([]A, 5)
for i := 0; i < len(a); i++ {
a[i].name = strconv.Itoa(i) + "A"
}
b := make([]B, 7)
for i := 0; i < len(b); i++ {
b[i].name = strconv.Itoa(i) + "B"
}
fmt.Println(Process(a))
fmt.Println(Process(b))
//Uncomment line below to see the panic
//fmt.Println(Process(8))
}
https://play.golang.org/p/irdCsbpvUv_t

Iterate Over String Fields in Struct

I'm looking to iterate over the string fields of a struct so I can do some clean-up/validation (with strings.TrimSpace, strings.Trim, etc).
Right now I have a messy switch-case that's not really scalable, and as this isn't in a hot spot of my application (a web form) it seems leveraging reflect is a good choice here.
I'm at a bit of a roadblock for how to implement this however, and the reflect docs are a little confusing to me (I've been digging through some other validation packages, but they're way too heavyweight + I'm using gorilla/schema for the unmarshalling part already):
Iterate over the struct
For each field of type string, apply whatever I need to from the strings package i.e. field = strings.TrimSpace(field)
If there exists a field.Tag.Get("max"), we'll use that value (strconv.Atoi, then unicode.RuneCountInString)
Provide an error slice that's also compatible with the error interface type
type FormError []string
type Listing struct {
Title string `max:"50"`
Location string `max:"100"`
Description string `max:"10000"`
ExpiryDate time.Time
RenderedDesc template.HTML
Contact string `max:"255"`
}
// Iterate over our struct, fix whitespace/formatting where possible
// and return errors encountered
func (l *Listing) Validate() error {
typ := l.Elem().Type()
var invalid FormError
for i = 0; i < typ.NumField(); i++ {
// Iterate over fields
// For StructFields of type string, field = strings.TrimSpace(field)
// if field.Tag.Get("max") != "" {
// check max length/convert to int/utf8.RuneCountInString
if max length exceeded, invalid = append(invalid, "errormsg")
}
if len(invalid) > 0 {
return invalid
}
return nil
}
func (f FormError) Error() string {
var fullError string
for _, v := range f {
fullError =+ v + "\n"
}
return "Errors were encountered during form processing: " + fullError
}
Thanks in advance.
What you want is primarily the methods on reflect.Value called NumFields() int and Field(int). The only thing you're really missing is the string check and SetString method.
package main
import "fmt"
import "reflect"
import "strings"
type MyStruct struct {
A,B,C string
I int
D string
J int
}
func main() {
ms := MyStruct{"Green ", " Eggs", " and ", 2, " Ham ", 15}
// Print it out now so we can see the difference
fmt.Printf("%s%s%s%d%s%d\n", ms.A, ms.B, ms.C, ms.I, ms.D, ms.J)
// We need a pointer so that we can set the value via reflection
msValuePtr := reflect.ValueOf(&ms)
msValue := msValuePtr.Elem()
for i := 0; i < msValue.NumField(); i++ {
field := msValue.Field(i)
// Ignore fields that don't have the same type as a string
if field.Type() != reflect.TypeOf("") {
continue
}
str := field.Interface().(string)
str = strings.TrimSpace(str)
field.SetString(str)
}
fmt.Printf("%s%s%s%d%s%d\n", ms.A, ms.B, ms.C, ms.I, ms.D, ms.J)
}
(Playground link)
There are two caveats here:
You need a pointer to what you're going to change. If you have a value, you'll need to return the modified result.
Attempts to modify unexported fields generally will cause reflect to panic. If you plan on modifying unexported fields, make sure to do this trick inside the package.
This code is rather flexible, you can use switch statements or type switches (on the value returned by field.Interface()) if you need differing behavior depending on the type.
Edit: As for the tag behavior, you seem to already have that figured out. Once you have field and have checked that it's a string, you can just use field.Tag.Get("max") and parse it from there.
Edit2: I made a small error on the tag. Tags are part of the reflect.Type of a struct, so to get them you can use (this is a bit long-winded) msValue.Type().Field(i).Tag.Get("max")
(Playground version of the code you posted in the comments with a working Tag get).
I got beat to the punch, but since I went to the work, here's a solution:
type FormError []*string
type Listing struct {
Title string `max:"50"`
Location string `max:"100"`
Description string `max:"10000"`
ExpiryDate time.Time
RenderedDesc template.HTML
Contact string `max:"255"`
}
// Iterate over our struct, fix whitespace/formatting where possible
// and return errors encountered
func (l *Listing) Validate() error {
listingType := reflect.TypeOf(*l)
listingValue := reflect.ValueOf(l)
listingElem := listingValue.Elem()
var invalid FormError = []*string{}
// Iterate over fields
for i := 0; i < listingElem.NumField(); i++ {
fieldValue := listingElem.Field(i)
// For StructFields of type string, field = strings.TrimSpace(field)
if fieldValue.Type().Name() == "string" {
newFieldValue := strings.TrimSpace(fieldValue.Interface().(string))
fieldValue.SetString(newFieldValue)
fieldType := listingType.Field(i)
maxLengthStr := fieldType.Tag.Get("max")
if maxLengthStr != "" {
maxLength, err := strconv.Atoi(maxLengthStr)
if err != nil {
panic("Field 'max' must be an integer")
}
// check max length/convert to int/utf8.RuneCountInString
if utf8.RuneCountInString(newFieldValue) > maxLength {
// if max length exceeded, invalid = append(invalid, "errormsg")
invalidMessage := `"`+fieldType.Name+`" is too long (max allowed: `+maxLengthStr+`)`
invalid = append(invalid, &invalidMessage)
}
}
}
}
if len(invalid) > 0 {
return invalid
}
return nil
}
func (f FormError) Error() string {
var fullError string
for _, v := range f {
fullError = *v + "\n"
}
return "Errors were encountered during form processing: " + fullError
}
I see you asked about how to do the tags. Reflection has two components: a type and a value. The tag is associated with the type, so you have to get it separately than the field: listingType := reflect.TypeOf(*l). Then you can get the indexed field and the tag from that.
I don't know if it's a good way, but I use it like this.
https://play.golang.org/p/aQ_hG2BYmMD
You can send the address of a struct to this function.
Sorry for My English is not very good.
trimStruct(&someStruct)
func trimStruct(v interface{}) {
bytes, err := json.Marshal(v)
if err != nil {
fmt.Println("[trimStruct] Marshal Error :", err)
}
var mapSI map[string]interface{}
if err := json.Unmarshal(bytes, &mapSI); err != nil {
fmt.Println("[trimStruct] Unmarshal to byte Error :", err)
}
mapSI = trimMapStringInterface(mapSI).(map[string]interface{})
bytes2, err := json.Marshal(mapSI)
if err != nil {
fmt.Println("[trimStruct] Marshal Error :", err)
}
if err := json.Unmarshal(bytes2, v); err != nil {
fmt.Println("[trimStruct] Unmarshal to b Error :", err)
}
}
func trimMapStringInterface(data interface{}) interface{} {
if values, valid := data.([]interface{}); valid {
for i := range values {
data.([]interface{})[i] = trimMapStringInterface(values[i])
}
} else if values, valid := data.(map[string]interface{}); valid {
for k, v := range values {
data.(map[string]interface{})[k] = trimMapStringInterface(v)
}
} else if value, valid := data.(string); valid {
data = strings.TrimSpace(value)
}
return data
}

Resources