Add struct tags flag

This commit is contained in:
Patrick O'brien 2016-09-04 23:44:54 +10:00
parent e35ecd76c1
commit 4e8191b8dd
7 changed files with 116 additions and 10 deletions

View file

@ -487,3 +487,35 @@ func ContainsAny(a []string, finds ...string) bool {
return false
}
// GenerateTags converts a slice of tag strings into tags that
// can be passed onto the end of a struct, for example:
// tags: ["xml", "db"] convert to: xml:"column_name" db:"column_name"
func GenerateTags(tags []string, columnName string) string {
buf := GetBuffer()
defer PutBuffer(buf)
for _, tag := range tags {
buf.WriteString(tag)
buf.WriteString(`:"`)
buf.WriteString(columnName)
buf.WriteString(`" `)
}
return buf.String()
}
// GenerateIgnoreTags converts a slice of tag strings into
// ignore tags that can be passed onto the end of a struct, for example:
// tags: ["xml", "db"] convert to: xml:"-" db:"-"
func GenerateIgnoreTags(tags []string) string {
buf := GetBuffer()
defer PutBuffer(buf)
for _, tag := range tags {
buf.WriteString(tag)
buf.WriteString(`:"-" `)
}
return buf.String()
}

View file

@ -426,3 +426,41 @@ func TestContainsAny(t *testing.T) {
t.Errorf("Should not return true")
}
}
func TestGenerateTags(t *testing.T) {
tags := GenerateTags([]string{}, "col_name")
if tags != "" {
t.Errorf("Expected empty string, got %s", tags)
}
tags = GenerateTags([]string{"xml"}, "col_name")
exp := `xml:"col_name" `
if tags != exp {
t.Errorf("expected %s, got %s", exp, tags)
}
tags = GenerateTags([]string{"xml", "db"}, "col_name")
exp = `xml:"col_name" db:"col_name" `
if tags != exp {
t.Errorf("expected %s, got %s", exp, tags)
}
}
func TestGenerateIgnoreTags(t *testing.T) {
tags := GenerateIgnoreTags([]string{})
if tags != "" {
t.Errorf("Expected empty string, got %s", tags)
}
tags = GenerateIgnoreTags([]string{"xml"})
exp := `xml:"-" `
if tags != exp {
t.Errorf("expected %s, got %s", exp, tags)
}
tags = GenerateIgnoreTags([]string{"xml", "db"})
exp = `xml:"-" db:"-" `
if tags != exp {
t.Errorf("expected %s, got %s", exp, tags)
}
}