github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/codegen/model.go (about)

     1  package codegen
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/ronaksoft/rony/tools"
     7  )
     8  
     9  /*
    10     Creation Time: 2021 - Jul - 29
    11     Created by:  (ehsan)
    12     Maintainers:
    13        1.  Ehsan N. Moosa (E2)
    14     Auditor: Ehsan N. Moosa (E2)
    15     Copyright Ronak Software Group 2020
    16  */
    17  
    18  type ModelKey struct {
    19  	Arg   *MessageArg
    20  	pks   []Prop
    21  	cks   []Prop
    22  	index int
    23  	alias string
    24  }
    25  
    26  func (m *ModelKey) Name() string {
    27  	return m.Arg.name
    28  }
    29  
    30  func (m *ModelKey) Alias() string {
    31  	return m.alias
    32  }
    33  
    34  func (m *ModelKey) PartitionKeys() []Prop {
    35  	var all []Prop
    36  	all = append(all, m.pks...)
    37  
    38  	return all
    39  }
    40  
    41  func (m *ModelKey) PKs() []Prop {
    42  	return m.PartitionKeys()
    43  }
    44  
    45  func (m *ModelKey) ClusteringKeys() []Prop {
    46  	var all []Prop
    47  	all = append(all, m.cks...)
    48  
    49  	return all
    50  }
    51  
    52  func (m *ModelKey) CKs() []Prop {
    53  	return m.ClusteringKeys()
    54  }
    55  
    56  func (m *ModelKey) Keys() []Prop {
    57  	var all []Prop
    58  	all = append(all, m.pks...)
    59  	all = append(all, m.cks...)
    60  
    61  	return all
    62  }
    63  
    64  func (m *ModelKey) Index() int {
    65  	return m.index
    66  }
    67  
    68  func (m *ModelKey) IsSubset(n *ModelKey) bool {
    69  	for _, np := range n.Keys() {
    70  		found := false
    71  		for _, mp := range m.Keys() {
    72  			if mp.Name == np.Name {
    73  				found = true
    74  
    75  				break
    76  			}
    77  		}
    78  		if !found {
    79  			return false
    80  		}
    81  	}
    82  
    83  	return true
    84  }
    85  
    86  func (m *ModelKey) HasProp(name string) bool {
    87  	for _, p := range m.Keys() {
    88  		if p.Name == name {
    89  			return true
    90  		}
    91  	}
    92  
    93  	return false
    94  }
    95  
    96  // NameTypes is kind of strings.Join function which returns a custom format of combination of model properties.
    97  // This is a helper function used in code generator templates.
    98  func (m *ModelKey) NameTypes(filter PropFilter, namePrefix string, nameCase TextCase, lang Language) string {
    99  	var props []Prop
   100  	switch filter {
   101  	case PropFilterALL:
   102  		props = m.Keys()
   103  	case PropFilterCKs:
   104  		props = m.ClusteringKeys()
   105  	case PropFilterPKs:
   106  		props = m.PartitionKeys()
   107  	}
   108  
   109  	sb := strings.Builder{}
   110  	for idx, p := range props {
   111  		if idx != 0 {
   112  			sb.WriteString(", ")
   113  		}
   114  
   115  		sb.WriteString(namePrefix)
   116  		switch nameCase {
   117  		case LowerCamelCase:
   118  			sb.WriteString(tools.ToLowerCamel(p.Name))
   119  		case CamelCase:
   120  			sb.WriteString(tools.ToCamel(p.Name))
   121  		case KebabCase:
   122  			sb.WriteString(tools.ToKebab(p.Name))
   123  		case SnakeCase:
   124  			sb.WriteString(tools.ToSnake(p.Name))
   125  		default:
   126  			sb.WriteString(p.Name)
   127  		}
   128  		sb.WriteRune(' ')
   129  		switch lang {
   130  		case LangGo:
   131  			sb.WriteString(p.GoType)
   132  		case LangCQL:
   133  			sb.WriteString(p.CqlType)
   134  		case LangProto:
   135  			sb.WriteString(p.ProtoType)
   136  		}
   137  	}
   138  
   139  	return sb.String()
   140  }
   141  
   142  // Names is kind of strings.Join function which returns a custom format of property names.
   143  // This is a helper function used in code generator templates.
   144  func (m *ModelKey) Names(filter PropFilter, prefix, postfix string, sep string, nameCase TextCase) string {
   145  	var props []Prop
   146  	switch filter {
   147  	case PropFilterALL:
   148  		props = m.Keys()
   149  	case PropFilterCKs:
   150  		props = m.ClusteringKeys()
   151  	case PropFilterPKs:
   152  		props = m.PartitionKeys()
   153  	}
   154  
   155  	sb := strings.Builder{}
   156  	for idx, p := range props {
   157  		if idx != 0 {
   158  			sb.WriteString(sep)
   159  		}
   160  		sb.WriteString(prefix)
   161  		switch nameCase {
   162  		case LowerCamelCase:
   163  			sb.WriteString(tools.ToLowerCamel(p.Name))
   164  		case CamelCase:
   165  			sb.WriteString(tools.ToCamel(p.Name))
   166  		case KebabCase:
   167  			sb.WriteString(tools.ToKebab(p.Name))
   168  		case SnakeCase:
   169  			sb.WriteString(tools.ToSnake(p.Name))
   170  		default:
   171  			sb.WriteString(p.Name)
   172  		}
   173  		sb.WriteString(postfix)
   174  	}
   175  
   176  	return sb.String()
   177  }
   178  
   179  type Prop struct {
   180  	Name      string
   181  	ProtoType string
   182  	CqlType   string
   183  	GoType    string
   184  	Order     Order
   185  }
   186  
   187  func (p *Prop) NameSC() string {
   188  	return tools.ToSnake(p.Name)
   189  }
   190  
   191  type PropFilter string
   192  
   193  const (
   194  	PropFilterALL = "ALL"
   195  	PropFilterPKs = "PKs"
   196  	PropFilterCKs = "CKs"
   197  )
   198  
   199  type Order string
   200  
   201  const (
   202  	ASC  Order = "ASC"
   203  	DESC Order = "DESC"
   204  )