github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/courier/client/gen/common/field.go (about)

     1  package common
     2  
     3  import (
     4  	"bytes"
     5  	"sort"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  func NewField(name string) *Field {
    11  	return &Field{
    12  		Name: name,
    13  	}
    14  }
    15  
    16  type Field struct {
    17  	Comment string
    18  	Name    string
    19  	Type    string
    20  	Tags    Tags
    21  }
    22  
    23  func (f *Field) AddTag(tagKey, tagValue string, flags ...string) {
    24  	if f.Tags == nil {
    25  		f.Tags = Tags{}
    26  	}
    27  	f.Tags[tagKey] = NewTag(tagKey, tagValue).WithFlags(flags...)
    28  }
    29  
    30  func AsCommentLines(comment string) string {
    31  	buf := &bytes.Buffer{}
    32  	for _, line := range strings.Split(comment, "\n") {
    33  		buf.WriteString(`// ` + line + `
    34  `)
    35  	}
    36  	return buf.String()
    37  }
    38  
    39  func (f *Field) Anonymous() bool {
    40  	return f.Type == ""
    41  }
    42  
    43  func (f *Field) String() string {
    44  	if f.Anonymous() {
    45  		return AsCommentLines(f.Comment) + f.Name + ` ` + f.Tags.String() + `
    46  `
    47  	}
    48  	return AsCommentLines(f.Comment) + f.Name + ` ` + f.Type + ` ` + f.Tags.String() + `
    49  `
    50  }
    51  
    52  func NewTag(key, value string) *Tag {
    53  	return &Tag{
    54  		Key:   key,
    55  		Value: value,
    56  	}
    57  }
    58  
    59  type Tag struct {
    60  	Key   string
    61  	Value string
    62  	Flags map[string]bool
    63  }
    64  
    65  func (tag Tag) WithFlags(flags ...string) *Tag {
    66  	for _, flag := range flags {
    67  		if tag.Flags == nil {
    68  			tag.Flags = map[string]bool{}
    69  		}
    70  		tag.Flags[flag] = true
    71  	}
    72  	return &tag
    73  }
    74  
    75  func (tag *Tag) String() string {
    76  	flags := make([]string, 0)
    77  	if len(tag.Flags) > 0 {
    78  		for flag := range tag.Flags {
    79  			flags = append(flags, flag)
    80  		}
    81  		sort.Strings(flags)
    82  	}
    83  
    84  	return tag.Key + ":" + strconv.Quote(strings.Join(append([]string{tag.Value}, flags...), ","))
    85  }
    86  
    87  type Tags map[string]*Tag
    88  
    89  func (tags Tags) String() string {
    90  	if len(tags) > 0 {
    91  		tagList := make([]string, 0)
    92  		for tag := range tags {
    93  			tagList = append(tagList, tag)
    94  		}
    95  		sort.Strings(tagList)
    96  
    97  		buf := &bytes.Buffer{}
    98  
    99  		tagCount := len(tagList)
   100  
   101  		for i, tag := range tagList {
   102  			if i == 0 {
   103  				buf.WriteString("`")
   104  			} else {
   105  				buf.WriteString(" ")
   106  			}
   107  
   108  			buf.WriteString(tags[tag].String())
   109  
   110  			if i == tagCount-1 {
   111  				buf.WriteString("`")
   112  			}
   113  		}
   114  		return buf.String()
   115  	}
   116  	return ""
   117  }