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

     1  package entity
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	schema "github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
     8  )
     9  
    10  // ColumnVarChar generated columns type for VarChar
    11  type ColumnVarChar struct {
    12  	ColumnBase
    13  	name   string
    14  	values []string
    15  }
    16  
    17  // Name returns column name
    18  func (c *ColumnVarChar) Name() string {
    19  	return c.name
    20  }
    21  
    22  // Type returns column FieldType
    23  func (c *ColumnVarChar) Type() FieldType {
    24  	return FieldTypeVarChar
    25  }
    26  
    27  // Len returns column values length
    28  func (c *ColumnVarChar) Len() int {
    29  	return len(c.values)
    30  }
    31  
    32  // Get returns value at index as interface{}.
    33  func (c *ColumnVarChar) Get(idx int) (interface{}, error) {
    34  	if idx < 0 || idx > c.Len() {
    35  		return "", errors.New("index out of range")
    36  	}
    37  	return c.values[idx], nil
    38  }
    39  
    40  // GetAsString returns value at idx.
    41  func (c *ColumnVarChar) GetAsString(idx int) (string, error) {
    42  	if idx < 0 || idx > c.Len() {
    43  		return "", errors.New("index out of range")
    44  	}
    45  	return c.values[idx], nil
    46  }
    47  
    48  func (c *ColumnVarChar) Slice(start, end int) Column {
    49  	l := c.Len()
    50  	if start > l {
    51  		start = l
    52  	}
    53  	if end == -1 || end > l {
    54  		end = l
    55  	}
    56  	return &ColumnVarChar{
    57  		ColumnBase: c.ColumnBase,
    58  		name:       c.name,
    59  		values:     c.values[start:end],
    60  	}
    61  }
    62  
    63  // FieldData return column data mapped to schema.FieldData
    64  func (c *ColumnVarChar) FieldData() *schema.FieldData {
    65  	fd := &schema.FieldData{
    66  		Type:      schema.DataType_VarChar,
    67  		FieldName: c.name,
    68  	}
    69  	data := make([]string, 0, c.Len())
    70  	for i := 0; i < c.Len(); i++ {
    71  		data = append(data, string(c.values[i]))
    72  	}
    73  	fd.Field = &schema.FieldData_Scalars{
    74  		Scalars: &schema.ScalarField{
    75  			Data: &schema.ScalarField_StringData{
    76  				StringData: &schema.StringArray{
    77  					Data: data,
    78  				},
    79  			},
    80  		},
    81  	}
    82  	return fd
    83  }
    84  
    85  // ValueByIdx returns value of the provided index
    86  // error occurs when index out of range
    87  func (c *ColumnVarChar) ValueByIdx(idx int) (string, error) {
    88  	var r string // use default value
    89  	if idx < 0 || idx >= c.Len() {
    90  		return r, errors.New("index out of range")
    91  	}
    92  	return c.values[idx], nil
    93  }
    94  
    95  // AppendValue append value into column
    96  func (c *ColumnVarChar) AppendValue(i interface{}) error {
    97  	v, ok := i.(string)
    98  	if !ok {
    99  		return fmt.Errorf("invalid type, expected string, got %T", i)
   100  	}
   101  	c.values = append(c.values, v)
   102  
   103  	return nil
   104  }
   105  
   106  // Data returns column data
   107  func (c *ColumnVarChar) Data() []string {
   108  	return c.values
   109  }
   110  
   111  // NewColumnVarChar auto generated constructor
   112  func NewColumnVarChar(name string, values []string) *ColumnVarChar {
   113  	return &ColumnVarChar{
   114  		name:   name,
   115  		values: values,
   116  	}
   117  }