github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/processors/query/factory-field-impl.go (about)

     1  /*
     2   * Copyright (c) 2021-present unTill Pro, Ltd.
     3   */
     4  
     5  package queryprocessor
     6  
     7  import "fmt"
     8  
     9  func NewField(data interface{}) (IField, error) {
    10  	switch result := data.(type) {
    11  	case string:
    12  		return resultField{result}, nil
    13  	case []interface{}:
    14  		return newRefField(result)
    15  	default:
    16  		return nil, fmt.Errorf("must be a sting or an array of strings: %w", ErrWrongType)
    17  	}
    18  }
    19  
    20  type resultField struct {
    21  	field string
    22  }
    23  
    24  func (f resultField) Field() string { return f.field }
    25  
    26  func newRefField(data []interface{}) (IField, error) {
    27  	if len(data) != 2 {
    28  		return nil, fmt.Errorf("field 'ref' parameters length must be 2 but got %d: %w", len(data), ErrWrongLength)
    29  	}
    30  	validate := func(intf interface{}) (string, error) {
    31  		v, ok := intf.(string)
    32  		if !ok {
    33  			return "", fmt.Errorf("field 'ref' parameter must a string: %w", ErrWrongType)
    34  		}
    35  		return v, nil
    36  	}
    37  	field, err := validate(data[0])
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	ref, err := validate(data[1])
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	f := refField{
    46  		field: field,
    47  		ref:   ref,
    48  	}
    49  	f.key = fmt.Sprintf("%s/%s", f.field, f.ref)
    50  	return f, nil
    51  }
    52  
    53  type refField struct {
    54  	field string
    55  	ref   string
    56  	key   string
    57  }
    58  
    59  func (f refField) Field() string    { return f.field }
    60  func (f refField) RefField() string { return f.ref }
    61  func (f refField) Key() string      { return f.key }