github.com/reggieriser/pop@v4.13.1+incompatible/columns/tags.go (about)

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