github.com/enbility/spine-go@v0.7.0/model/eebus_tags.go (about)

     1  package model
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  
     7  	"github.com/enbility/ship-go/logging"
     8  )
     9  
    10  type EEBusTag string
    11  
    12  const (
    13  	EEBusTagFunction   EEBusTag = "fct"
    14  	EEBusTagType       EEBusTag = "typ"
    15  	EEBusTagKey        EEBusTag = "key"
    16  	EEBusTagWriteCheck EEBusTag = "writecheck"
    17  )
    18  
    19  type EEBusTagTypeType string
    20  
    21  const (
    22  	EEBusTagTypeTypeSelector EEBusTagTypeType = "selector"
    23  	EEbusTagTypeTypeElements EEBusTagTypeType = "elements"
    24  )
    25  
    26  const EEBusTagName string = "eebus"
    27  
    28  func EEBusTags(field reflect.StructField) map[EEBusTag]string {
    29  	result := make(map[EEBusTag]string)
    30  	tags := field.Tag.Get(EEBusTagName)
    31  	if len(tags) == 0 {
    32  		return result
    33  	}
    34  	for _, tag := range strings.Split(tags, ",") {
    35  		pair := strings.Split(tag, ":")
    36  		if len(pair) == 1 {
    37  			// boolean tags like "key"
    38  			result[EEBusTag(pair[0])] = "true"
    39  		} else if len(pair) == 2 {
    40  			result[EEBusTag(pair[0])] = pair[1]
    41  		} else {
    42  			logging.Log().Errorf("error: malformatted eebus tag: '%s'", tags)
    43  		}
    44  	}
    45  
    46  	return result
    47  }