github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/gen/gen.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // Copyright (C) 2019-2021 Zilliz. All rights reserved.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
     7  // with the License. You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software distributed under the License
    12  // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
    13  // or implied. See the License for the specific language governing permissions and limitations under the License.
    14  
    15  // This program generates entity/columns_{{FieldType}}.go. Invoked by go generate
    16  package main
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"text/template"
    22  
    23  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
    24  )
    25  
    26  var scalarColumnTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT
    27  // This file is generated by go generate 
    28  
    29  package entity 
    30  
    31  import (
    32  	"errors"
    33  	"fmt"
    34  
    35  	schema "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
    36  )
    37  {{ range .Types }}{{with .}}
    38  // Column{{.TypeName}} generated columns type for {{.TypeName}}
    39  type Column{{.TypeName}} struct {
    40  	ColumnBase
    41  	name   string
    42  	values []{{.TypeDef}}
    43  }
    44  
    45  // Name returns column name
    46  func (c *Column{{.TypeName}}) Name() string {
    47  	return c.name
    48  }
    49  
    50  // Type returns column FieldType
    51  func (c *Column{{.TypeName}}) Type() FieldType {
    52  	return FieldType{{.TypeName}}
    53  }
    54  
    55  // Len returns column values length
    56  func (c *Column{{.TypeName}}) Len() int {
    57  	return len(c.values)
    58  }
    59  
    60  // Get returns value at index as interface{}.
    61  func (c *Column{{.TypeName}}) Get(idx int) (interface{}, error) {
    62  	var r {{.TypeDef}} // use default value
    63  	if idx < 0 || idx >= c.Len() {
    64  		return r, errors.New("index out of range")
    65  	}
    66  	return c.values[idx], nil
    67  }
    68  
    69  // FieldData return column data mapped to schema.FieldData
    70  func (c *Column{{.TypeName}}) FieldData() *schema.FieldData {
    71  	fd := &schema.FieldData{
    72  		Type: schema.DataType_{{.TypeName}},
    73  		FieldName: c.name,
    74  	}
    75  	data := make([]{{.PbType}}, 0, c.Len())
    76  	for i := 0; i < c.Len(); i++ {
    77  		data = append(data, {{.PbType}}(c.values[i]))
    78  	}
    79  	fd.Field = &schema.FieldData_Scalars{
    80  		Scalars: &schema.ScalarField{
    81  			Data: &schema.ScalarField_{{.PbName}}Data{
    82  				{{.PbName}}Data: &schema.{{.PbName}}Array{
    83  					Data: data,
    84  				},
    85  			},
    86  		},
    87  	}
    88  	return fd
    89  }
    90  
    91  // ValueByIdx returns value of the provided index
    92  // error occurs when index out of range
    93  func (c *Column{{.TypeName}}) ValueByIdx(idx int) ({{.TypeDef}}, error) {
    94  	var r {{.TypeDef}} // use default value
    95  	if idx < 0 || idx >= c.Len() {
    96  		return r, errors.New("index out of range")
    97  	}
    98  	return c.values[idx], nil
    99  }
   100  
   101  // AppendValue append value into column
   102  func(c *Column{{.TypeName}}) AppendValue(i interface{}) error {
   103  	v, ok := i.({{.TypeDef}})
   104  	if !ok {
   105  		return fmt.Errorf("invalid type, expected {{.TypeDef}}, got %T", i)
   106  	}
   107  	c.values = append(c.values, v)
   108  
   109  	return nil
   110  }
   111  
   112  // Data returns column data
   113  func (c *Column{{.TypeName}}) Data() []{{.TypeDef}} {
   114  	return c.values
   115  }
   116  
   117  // NewColumn{{.TypeName}} auto generated constructor
   118  func NewColumn{{.TypeName}}(name string, values []{{.TypeDef}}) *Column{{.TypeName}} {
   119  	return &Column{{.TypeName}} {
   120  		name: name,
   121  		values: values,
   122  	}
   123  }
   124  {{end}}{{end}}
   125  `))
   126  var vectorColumnTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT
   127  // This file is generated by go generated
   128  package entity
   129  
   130  import (
   131  	"fmt"
   132  
   133  	schema "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
   134  
   135  	"github.com/cockroachdb/errors"
   136  )
   137  
   138  {{ range .Types }}{{with.}}
   139  // Column{{.TypeName}} generated columns type for {{.TypeName}}
   140  type Column{{.TypeName}} struct {
   141  	ColumnBase
   142  	name   string
   143  	dim    int
   144  	values []{{.TypeDef}}
   145  }
   146  
   147  // Name returns column name
   148  func (c *Column{{.TypeName}}) Name() string {
   149  	return c.name
   150  }
   151  
   152  // Type returns column FieldType
   153  func (c *Column{{.TypeName}}) Type() FieldType {
   154  	return FieldType{{.TypeName}}
   155  }
   156  
   157  // Len returns column data length
   158  func (c * Column{{.TypeName}}) Len() int {
   159  	return len(c.values)
   160  }
   161  
   162  // Dim returns vector dimension
   163  func (c *Column{{.TypeName}}) Dim() int {
   164  	return c.dim
   165  }
   166  
   167  // Get returns values at index as interface{}.
   168  func (c *Column{{.TypeName}}) Get(idx int) (interface{}, error) {
   169  	if idx < 0 || idx >= c.Len() {
   170  		return nil, errors.New("index out of range")
   171  	}
   172  	return c.values[idx], nil
   173  }
   174  
   175  // AppendValue append value into column
   176  func(c *Column{{.TypeName}}) AppendValue(i interface{}) error {
   177  	v, ok := i.({{.TypeDef}})
   178  	if !ok {
   179  		return fmt.Errorf("invalid type, expected {{.TypeDef}}, got %T", i)
   180  	}
   181  	c.values = append(c.values, v)
   182  
   183  	return nil
   184  }
   185  
   186  // Data returns column data
   187  func (c *Column{{.TypeName}}) Data() []{{.TypeDef}} {
   188  	return c.values
   189  }
   190  
   191  // FieldData return column data mapped to schema.FieldData
   192  func (c *Column{{.TypeName}}) FieldData() *schema.FieldData {
   193  	fd := &schema.FieldData{
   194  		Type: schema.DataType_{{.TypeName}},
   195  		FieldName: c.name,
   196  	}
   197  
   198  	data := make({{.TypeDef}}, 0, len(c.values)* c.dim)
   199  
   200  	for _, vector := range c.values {
   201  		data = append(data, vector...)
   202  	}
   203  
   204  	fd.Field = &schema.FieldData_Vectors{
   205  		Vectors: &schema.VectorField{
   206  			Dim: int64(c.dim),
   207  			{{if eq .TypeName "FloatVector" }}
   208  			Data: &schema.VectorField_{{.TypeName}}{
   209  				FloatVector: &schema.FloatArray{
   210  					Data: data,
   211  				},
   212  			{{else if eq .TypeName "BFloat16Vector"}}
   213  			Data: &schema.VectorField_Bfloat16Vector{
   214  				Bfloat16Vector: data,
   215  			{{else}}
   216  			Data: &schema.VectorField_{{.TypeName}}{				
   217  				{{.TypeName}}: data,
   218  			{{end}}
   219  			},
   220  		},
   221  	}
   222  	return fd
   223  }
   224  
   225  // NewColumn{{.TypeName}} auto generated constructor
   226  func NewColumn{{.TypeName}}(name string, dim int, values []{{.TypeDef}}) *Column{{.TypeName}} {
   227  	return &Column{{.TypeName}} {
   228  		name:   name,
   229  		dim:    dim,
   230  		values: values,
   231  	}
   232  }
   233  {{end}}{{end}}
   234  `))
   235  
   236  var scalarColumnTestTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT
   237  // This file is generated by go generated 
   238  
   239  package entity 
   240  
   241  import (
   242  	"fmt"
   243  	"math/rand"
   244  	"testing"
   245  	"time"
   246  
   247  	"github.com/stretchr/testify/assert"
   248  	schema "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
   249  )
   250  {{ range .Types }}{{with.}}
   251  func TestColumn{{.TypeName}}(t *testing.T) {
   252  	rand.Seed(time.Now().UnixNano())
   253  	columnName := fmt.Sprintf("column_{{.TypeName}}_%d", rand.Int())
   254  	columnLen := 8 + rand.Intn(10)
   255  
   256  	v := make([]{{.TypeDef}}, columnLen)
   257  	column := NewColumn{{.TypeName}}(columnName, v)
   258  
   259  	t.Run("test meta", func(t *testing.T) {
   260  		ft := FieldType{{.TypeName}}
   261  		assert.Equal(t, "{{.TypeName}}", ft.Name())
   262  		assert.Equal(t, "{{.TypeDef}}", ft.String())
   263  		pbName, pbType := ft.PbFieldType()
   264  		assert.Equal(t, "{{.PbName}}", pbName)
   265  		assert.Equal(t, "{{.PbType}}", pbType)
   266  	})
   267  
   268  	t.Run("test column attribute", func(t *testing.T) {
   269  		assert.Equal(t, columnName, column.Name())
   270  		assert.Equal(t, FieldType{{.TypeName}}, column.Type())
   271  		assert.Equal(t, columnLen, column.Len())
   272  		assert.EqualValues(t, v, column.Data())
   273  	})
   274  
   275  	t.Run("test column field data", func(t *testing.T) {
   276  		fd := column.FieldData()
   277  		assert.NotNil(t, fd)
   278  		assert.Equal(t, fd.GetFieldName(), columnName)
   279  	})
   280  
   281  	t.Run("test column value by idx", func(t *testing.T) {
   282  		_, err := column.ValueByIdx(-1)
   283  		assert.NotNil(t, err)
   284  		_, err = column.ValueByIdx(columnLen)
   285  		assert.NotNil(t, err)
   286  		for i := 0; i < columnLen; i++ {
   287  			v, err := column.ValueByIdx(i)
   288  			assert.Nil(t, err)
   289  			assert.Equal(t, column.values[i], v)
   290  		}
   291  	})
   292  }
   293  
   294  func TestFieldData{{.TypeName}}Column(t *testing.T) {
   295  	len := rand.Intn(10) + 8
   296  	name := fmt.Sprintf("fd_{{.TypeName}}_%d", rand.Int())
   297  	fd := &schema.FieldData{
   298  		Type: schema.DataType_{{.TypeName}},
   299  		FieldName: name,
   300  	}
   301  
   302  	t.Run("normal usage", func(t *testing.T) {
   303  		fd.Field = &schema.FieldData_Scalars{
   304  			Scalars: &schema.ScalarField{
   305  				Data: &schema.ScalarField_{{.PbName}}Data{
   306  					{{.PbName}}Data: &schema.{{.PbName}}Array{
   307  						Data: make([]{{.PbType}}, len),
   308  					},
   309  				},
   310  			},
   311  		}
   312  		column, err := FieldDataColumn(fd, 0, len)
   313  		assert.Nil(t, err)
   314  		assert.NotNil(t, column)
   315   
   316  		assert.Equal(t, name, column.Name())
   317  		assert.Equal(t, len, column.Len())
   318  		assert.Equal(t, FieldType{{.TypeName}}, column.Type())
   319  
   320  		var ev {{.TypeDef}}
   321  		err = column.AppendValue(ev)
   322  		assert.Equal(t, len+1, column.Len())
   323  		assert.Nil(t, err)
   324  
   325  		err = column.AppendValue(struct{}{})
   326  		assert.Equal(t, len+1, column.Len())
   327  		assert.NotNil(t, err)
   328  	})
   329  
   330  	
   331  	t.Run("nil data", func(t *testing.T) {
   332  		fd.Field = nil
   333  		_, err := FieldDataColumn(fd, 0, len)
   334  		assert.NotNil(t, err)
   335  	})
   336  	
   337  	t.Run("get all data", func(t *testing.T) {
   338  		fd.Field = &schema.FieldData_Scalars{
   339  			Scalars: &schema.ScalarField{
   340  				Data: &schema.ScalarField_{{.PbName}}Data{
   341  					{{.PbName}}Data: &schema.{{.PbName}}Array{
   342  						Data: make([]{{.PbType}}, len),
   343  					},
   344  				},
   345  			},
   346  		}
   347  		column, err := FieldDataColumn(fd, 0, -1)
   348  		assert.Nil(t, err)
   349  		assert.NotNil(t, column)
   350  		
   351  		assert.Equal(t, name, column.Name())
   352  		assert.Equal(t, len, column.Len())
   353  		assert.Equal(t, FieldType{{.TypeName}}, column.Type())
   354  	})
   355  }
   356  {{end}}{{end}}
   357  `))
   358  var vectorColumnTestTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT
   359  // This file is generated by go generated 
   360  
   361  package entity 
   362  
   363  import (
   364  	"fmt"
   365  	"math/rand"
   366  	"testing"
   367  	"time"
   368  
   369  	schema "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
   370  	"github.com/stretchr/testify/assert"
   371  )
   372  {{range .Types}}{{with .}}
   373  func TestColumn{{.TypeName}}(t *testing.T) {
   374  	rand.Seed(time.Now().UnixNano())
   375  	columnName := fmt.Sprintf("column_{{.TypeName}}_%d", rand.Int())
   376  	columnLen := 12 + rand.Intn(10)
   377  	dim := ([]int{64, 128, 256, 512})[rand.Intn(4)]
   378  
   379  	v := make([]{{.TypeDef}},0, columnLen)
   380  	dlen := dim
   381  	{{if eq .TypeName "BinaryVector" }}dlen /= 8{{end}}
   382  	{{if or (eq .TypeName "BFloat16Vector") (eq .TypeName "Float16Vector") }}dlen *= 2{{end}}
   383  	
   384  	for i := 0; i < columnLen; i++ {
   385  		entry := make({{.TypeDef}}, dlen)
   386  		v = append(v, entry)
   387  	}
   388  	column := NewColumn{{.TypeName}}(columnName, dim, v)
   389  	
   390  	t.Run("test meta", func(t *testing.T) {
   391  		ft := FieldType{{.TypeName}}
   392  		assert.Equal(t, "{{.TypeName}}", ft.Name())
   393  		assert.Equal(t, "{{.TypeDef}}", ft.String())
   394  		pbName, pbType := ft.PbFieldType()
   395  		assert.Equal(t, "{{.PbName}}", pbName)
   396  		assert.Equal(t, "{{.PbType}}", pbType)
   397  	})
   398  
   399  	t.Run("test column attribute", func(t *testing.T) {
   400  		assert.Equal(t, columnName, column.Name())
   401  		assert.Equal(t, FieldType{{.TypeName}}, column.Type())
   402  		assert.Equal(t, columnLen, column.Len())
   403  		assert.Equal(t, dim, column.Dim())
   404  		assert.Equal(t ,v, column.Data())
   405  		
   406  		var ev {{.TypeDef}}
   407  		err := column.AppendValue(ev)
   408  		assert.Equal(t, columnLen+1, column.Len())
   409  		assert.Nil(t, err)
   410  		
   411  		err = column.AppendValue(struct{}{})
   412  		assert.Equal(t, columnLen+1, column.Len())
   413  		assert.NotNil(t, err)
   414  	})
   415  
   416  	t.Run("test column field data", func(t *testing.T) {
   417  		fd := column.FieldData()
   418  		assert.NotNil(t, fd)
   419  		assert.Equal(t, fd.GetFieldName(), columnName)
   420  
   421  		c, err := FieldDataVector(fd)
   422  		assert.NotNil(t, c)
   423  		assert.NoError(t, err)
   424  	})
   425  
   426  	t.Run("test column field data error", func(t *testing.T) {
   427  		fd := &schema.FieldData{
   428  			Type:      schema.DataType_{{.TypeName}},
   429  			FieldName: columnName,
   430  		}
   431  		_, err := FieldDataVector(fd) 
   432  		assert.Error(t, err)
   433  	})
   434  
   435  }
   436  {{end}}{{end}}
   437  `))
   438  
   439  func main() {
   440  	scalarFieldTypes := []entity.FieldType{
   441  		entity.FieldTypeBool,
   442  		entity.FieldTypeInt8,
   443  		entity.FieldTypeInt16,
   444  		entity.FieldTypeInt32,
   445  		entity.FieldTypeInt64,
   446  		entity.FieldTypeFloat,
   447  		entity.FieldTypeDouble,
   448  		entity.FieldTypeString,
   449  	}
   450  	vectorFieldTypes := []entity.FieldType{
   451  		entity.FieldTypeBinaryVector,
   452  		entity.FieldTypeFloatVector,
   453  		entity.FieldTypeFloat16Vector,
   454  		entity.FieldTypeBFloat16Vector,
   455  	}
   456  
   457  	pf := func(ft entity.FieldType) interface{} {
   458  		pbName, pbType := ft.PbFieldType()
   459  		return struct {
   460  			TypeName string
   461  			TypeDef  string
   462  			PbName   string
   463  			PbType   string
   464  		}{
   465  			TypeName: ft.Name(),
   466  			TypeDef:  ft.String(),
   467  			PbName:   pbName,
   468  			PbType:   pbType,
   469  		}
   470  	}
   471  	fn := func(fn string, types []entity.FieldType, tmpl *template.Template, pf func(entity.FieldType) interface{}) {
   472  		params := struct {
   473  			Types []interface{}
   474  		}{
   475  			Types: make([]interface{}, 0, len(types)),
   476  		}
   477  		for _, ft := range types {
   478  			params.Types = append(params.Types, pf(ft))
   479  		}
   480  		f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
   481  		if err != nil {
   482  			fmt.Println(err.Error())
   483  			return
   484  		}
   485  		defer f.Close()
   486  
   487  		tmpl.Execute(f, params)
   488  	}
   489  	fnTest := func(fn string, types []entity.FieldType, tmpl *template.Template, pf func(entity.FieldType) interface{}) {
   490  		params := struct {
   491  			Types []interface{}
   492  		}{
   493  			Types: make([]interface{}, 0, len(types)),
   494  		}
   495  		for _, ft := range types {
   496  			params.Types = append(params.Types, pf(ft))
   497  		}
   498  		f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
   499  		if err != nil {
   500  			fmt.Println(err.Error())
   501  			return
   502  		}
   503  		defer f.Close()
   504  		tmpl.Execute(f, params)
   505  	}
   506  	fn("columns_scalar_gen.go", scalarFieldTypes, scalarColumnTemplate, pf)
   507  	fn("columns_vector_gen.go", vectorFieldTypes, vectorColumnTemplate, pf)
   508  	fnTest("columns_scalar_gen_test.go", scalarFieldTypes, scalarColumnTestTemplate, pf)
   509  	fnTest("columns_vector_gen_test.go", vectorFieldTypes, vectorColumnTestTemplate, pf)
   510  }