github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/genarray/gen_array.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "text/template" 7 8 "github.com/milvus-io/milvus-sdk-go/v2/entity" 9 ) 10 11 var arrayColumnTmpl = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT 12 // This file is generated by go generate 13 14 package entity 15 16 import ( 17 "fmt" 18 19 "github.com/cockroachdb/errors" 20 "github.com/milvus-io/milvus-proto/go-api/v2/schemapb" 21 ) 22 23 {{ range .Types }}{{with .}} 24 // Column{{.TypeName}}Array generated columns type for {{.TypeName}} 25 type Column{{.TypeName}}Array struct { 26 ColumnBase 27 name string 28 values [][]{{.TypeDef}} 29 } 30 31 // Name returns column name 32 func (c *Column{{.TypeName}}Array) Name() string { 33 return c.name 34 } 35 36 // Type returns column FieldType 37 func (c *Column{{.TypeName}}Array) Type() FieldType { 38 return FieldTypeArray 39 } 40 41 // Len returns column values length 42 func (c *Column{{.TypeName}}Array) Len() int { 43 return len(c.values) 44 } 45 46 // Get returns value at index as interface{}. 47 func (c *Column{{.TypeName}}Array) Get(idx int) (interface{}, error) { 48 var r []{{.TypeDef}} // use default value 49 if idx < 0 || idx >= c.Len() { 50 return r, errors.New("index out of range") 51 } 52 return c.values[idx], nil 53 } 54 55 // FieldData return column data mapped to schemapb.FieldData 56 func (c *Column{{.TypeName}}Array) FieldData() *schemapb.FieldData { 57 fd := &schemapb.FieldData{ 58 Type: schemapb.DataType_Array, 59 FieldName: c.name, 60 } 61 62 data := make([]*schemapb.ScalarField, 0, c.Len()) 63 for _, arr := range c.values { 64 converted := make([]{{.PbType}}, 0, c.Len()) 65 for i := 0; i < len(arr); i++ { 66 converted = append(converted, {{.PbType}}(arr[i])) 67 } 68 data = append(data, &schemapb.ScalarField{ 69 Data: &schemapb.ScalarField_{{.PbName}}Data{ 70 {{.PbName}}Data: &schemapb.{{.PbName}}Array{ 71 Data: converted, 72 }, 73 }, 74 }) 75 } 76 fd.Field = &schemapb.FieldData_Scalars{ 77 Scalars: &schemapb.ScalarField{ 78 Data: &schemapb.ScalarField_ArrayData{ 79 ArrayData: &schemapb.ArrayArray{ 80 Data: data, 81 ElementType: schemapb.DataType_{{.TypeName}}, 82 }, 83 }, 84 }, 85 } 86 return fd 87 } 88 89 // ValueByIdx returns value of the provided index 90 // error occurs when index out of range 91 func (c *Column{{.TypeName}}Array) ValueByIdx(idx int) ([]{{.TypeDef}}, error) { 92 var r []{{.TypeDef}} // use default value 93 if idx < 0 || idx >= c.Len() { 94 return r, errors.New("index out of range") 95 } 96 return c.values[idx], nil 97 } 98 99 // AppendValue append value into column 100 func(c *Column{{.TypeName}}Array) AppendValue(i interface{}) error { 101 v, ok := i.([]{{.TypeDef}}) 102 if !ok { 103 return fmt.Errorf("invalid type, expected []{{.TypeDef}}, got %T", i) 104 } 105 c.values = append(c.values, v) 106 107 return nil 108 } 109 110 // Data returns column data 111 func (c *Column{{.TypeName}}Array) Data() [][]{{.TypeDef}} { 112 return c.values 113 } 114 115 // NewColumn{{.TypeName}} auto generated constructor 116 func NewColumn{{.TypeName}}Array(name string, values [][]{{.TypeDef}}) *Column{{.TypeName}}Array { 117 return &Column{{.TypeName}}Array { 118 name: name, 119 values: values, 120 } 121 } 122 {{end}}{{end}} 123 `)) 124 125 func main() { 126 arrElementTypes := []entity.FieldType{ 127 entity.FieldTypeBool, 128 entity.FieldTypeInt8, 129 entity.FieldTypeInt16, 130 entity.FieldTypeInt32, 131 entity.FieldTypeInt64, 132 entity.FieldTypeFloat, 133 entity.FieldTypeDouble, 134 // entity.FieldTypeVarChar, change to hand written 135 } 136 137 pf := func(ft entity.FieldType) interface{} { 138 pbName, pbType := ft.PbFieldType() 139 return struct { 140 TypeName string 141 TypeDef string 142 PbName string 143 PbType string 144 }{ 145 TypeName: ft.Name(), 146 TypeDef: ft.String(), 147 PbName: pbName, 148 PbType: pbType, 149 } 150 } 151 152 fn := func(fn string, types []entity.FieldType, tmpl *template.Template, pf func(entity.FieldType) interface{}) { 153 params := struct { 154 Types []interface{} 155 }{ 156 Types: make([]interface{}, 0, len(types)), 157 } 158 for _, ft := range types { 159 params.Types = append(params.Types, pf(ft)) 160 } 161 f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) 162 if err != nil { 163 fmt.Println(err.Error()) 164 return 165 } 166 defer f.Close() 167 168 tmpl.Execute(f, params) 169 } 170 fn("columns_array_gen.go", arrElementTypes, arrayColumnTmpl, pf) 171 }