github.com/dkishere/pop/v6@v6.103.1/columns/tags.go (about)

     1  package columns
     2  
     3  import (
     4  	"github.com/gobeam/stringy"
     5  	"reflect"
     6  	"strings"
     7  )
     8  
     9  var tags = "db rw select belongs_to has_many has_one fk_id primary_id order_by many_to_many"
    10  
    11  // Tag represents a field tag defined exclusively for pop package.
    12  type Tag struct {
    13  	Value string
    14  	Name  string
    15  }
    16  
    17  // Empty validates if this pop tag is empty.
    18  func (t Tag) Empty() bool {
    19  	return t.Value == ""
    20  }
    21  
    22  // Ignored validates if this pop tag is ignored.
    23  // assuming an ignored tag as "-".
    24  func (t Tag) Ignored() bool {
    25  	return t.Value == "-"
    26  }
    27  
    28  // Tags is a group of pop tags defined in just one model field.
    29  type Tags []Tag
    30  
    31  // Find find for a specific tag with the name passed as
    32  // a param. returns an empty Tag in case it is not found.
    33  func (t Tags) Find(name string) Tag {
    34  	for _, popTag := range t {
    35  		if popTag.Name == name {
    36  			return popTag
    37  		}
    38  	}
    39  	return Tag{}
    40  }
    41  
    42  // TagsFor is a function which returns all tags defined
    43  // in model field.
    44  func TagsFor(field reflect.StructField) Tags {
    45  	pTags := Tags{}
    46  	for _, tag := range strings.Fields(tags) {
    47  		if valTag := field.Tag.Get(tag); valTag != "" {
    48  			pTags = append(pTags, Tag{valTag, tag})
    49  		}
    50  	}
    51  
    52  	if len(pTags) == 0 {
    53  		pTags = append(pTags, Tag{stringy.New(field.Name).SnakeCase("?", "").ToLower(), "db"})
    54  	}
    55  	return pTags
    56  }