code.vegaprotocol.io/vega@v0.79.0/core/datasource/common/spec.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  //lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
    17  
    18  package common
    19  
    20  import (
    21  	"fmt"
    22  	"strconv"
    23  	"strings"
    24  
    25  	vegapb "code.vegaprotocol.io/vega/protos/vega"
    26  	datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
    27  )
    28  
    29  type SpecConditionOperator = datapb.Condition_Operator
    30  
    31  // SpecCondition mirrors datapb.Condition type.
    32  type SpecCondition struct {
    33  	Operator SpecConditionOperator
    34  	Value    string
    35  }
    36  
    37  func (c SpecCondition) String() string {
    38  	return fmt.Sprintf(
    39  		"value(%s) operator(%s)",
    40  		c.Value,
    41  		c.Operator.String(),
    42  	)
    43  }
    44  
    45  func (c SpecCondition) IntoProto() *datapb.Condition {
    46  	return &datapb.Condition{
    47  		Operator: c.Operator,
    48  		Value:    c.Value,
    49  	}
    50  }
    51  
    52  func (c *SpecCondition) DeepClone() *SpecCondition {
    53  	return &SpecCondition{
    54  		Operator: c.Operator,
    55  		Value:    c.Value,
    56  	}
    57  }
    58  
    59  func SpecConditionFromProto(protoCondition *datapb.Condition) *SpecCondition {
    60  	return &SpecCondition{
    61  		Operator: protoCondition.Operator,
    62  		Value:    protoCondition.Value,
    63  	}
    64  }
    65  
    66  type SpecConditions []*SpecCondition
    67  
    68  func (sc SpecConditions) IntoProto() []*datapb.Condition {
    69  	protoConditions := []*datapb.Condition{}
    70  	for _, condition := range sc {
    71  		protoConditions = append(protoConditions, condition.IntoProto())
    72  	}
    73  	return protoConditions
    74  }
    75  
    76  func (sc SpecConditions) String() string {
    77  	if sc == nil {
    78  		return "[]"
    79  	}
    80  	strs := []string{}
    81  	for _, c := range sc {
    82  		strs = append(strs, c.String())
    83  	}
    84  	return "[" + strings.Join(strs, ", ") + "]"
    85  }
    86  
    87  func SpecConditionsFromProto(protoConditions []*datapb.Condition) []*SpecCondition {
    88  	conditions := []*SpecCondition{}
    89  	for _, protoCondition := range protoConditions {
    90  		conditions = append(conditions, SpecConditionFromProto(protoCondition))
    91  	}
    92  	return conditions
    93  }
    94  
    95  func DeepCloneSpecConditions(conditions []*SpecCondition) []*SpecCondition {
    96  	othConditions := make([]*SpecCondition, 0, len(conditions))
    97  	for _, condition := range conditions {
    98  		othConditions = append(othConditions, condition.DeepClone())
    99  	}
   100  	return othConditions
   101  }
   102  
   103  type SpecFilter struct {
   104  	Key        *SpecPropertyKey
   105  	Conditions []*SpecCondition
   106  }
   107  
   108  func SpecFilterFromProto(protoFilter *datapb.Filter) *SpecFilter {
   109  	filter := &SpecFilter{
   110  		Key:        &SpecPropertyKey{},
   111  		Conditions: []*SpecCondition{},
   112  	}
   113  
   114  	if protoFilter.Key != nil {
   115  		*filter.Key = *SpecPropertyKeyFromProto(protoFilter.Key)
   116  	}
   117  
   118  	filter.Conditions = SpecConditionsFromProto(protoFilter.Conditions)
   119  	return filter
   120  }
   121  
   122  func (f SpecFilter) String() string {
   123  	return fmt.Sprintf(
   124  		"key(%s) conditions(%v)",
   125  		f.Key.String(),
   126  		SpecConditions(f.Conditions).String(),
   127  	)
   128  }
   129  
   130  // IntoProto return proto version of the filter receiver
   131  // taking into account if its fields are empty.
   132  func (f *SpecFilter) IntoProto() *datapb.Filter {
   133  	filter := &datapb.Filter{
   134  		Key:        &datapb.PropertyKey{},
   135  		Conditions: []*datapb.Condition{},
   136  	}
   137  	if f.Key != nil {
   138  		filter.Key = f.Key.IntoProto()
   139  	}
   140  
   141  	if len(f.Conditions) > 0 {
   142  		filter.Conditions = SpecConditions(f.Conditions).IntoProto()
   143  	}
   144  	return filter
   145  }
   146  
   147  // DeepClone clones the filter receiver taking into account if its fields are empty.
   148  func (f *SpecFilter) DeepClone() *SpecFilter {
   149  	filter := &SpecFilter{
   150  		Key:        &SpecPropertyKey{},
   151  		Conditions: []*SpecCondition{},
   152  	}
   153  	if f.Key != nil {
   154  		filter.Key = f.Key.DeepClone()
   155  	}
   156  
   157  	if len(f.Conditions) > 0 {
   158  		filter.Conditions = DeepCloneSpecConditions(f.Conditions)
   159  	}
   160  	return filter
   161  }
   162  
   163  type SpecFilters []*SpecFilter
   164  
   165  func (df SpecFilters) IntoProto() []*datapb.Filter {
   166  	protoFilters := make([]*datapb.Filter, 0, len(df))
   167  	for _, filter := range df {
   168  		protoFilters = append(protoFilters, filter.IntoProto())
   169  	}
   170  	return protoFilters
   171  }
   172  
   173  func (df SpecFilters) String() string {
   174  	if df == nil {
   175  		return "[]"
   176  	}
   177  	strs := make([]string, 0, len(df))
   178  	for _, f := range df {
   179  		strs = append(strs, f.String())
   180  	}
   181  	return "[" + strings.Join(strs, ", ") + "]"
   182  }
   183  
   184  func SpecFiltersFromProto(protoFilters []*datapb.Filter) []*SpecFilter {
   185  	dsf := make([]*SpecFilter, len(protoFilters))
   186  	for i, protoFilter := range protoFilters {
   187  		dsf[i] = SpecFilterFromProto(protoFilter)
   188  	}
   189  	return dsf
   190  }
   191  
   192  func DeepCloneSpecFilters(filters []*SpecFilter) []*SpecFilter {
   193  	clonedFilters := make([]*SpecFilter, 0, len(filters))
   194  	for _, filter := range filters {
   195  		clonedFilters = append(clonedFilters, filter.DeepClone())
   196  	}
   197  	return clonedFilters
   198  }
   199  
   200  type SpecPropertyKeyType = datapb.PropertyKey_Type
   201  
   202  type SpecStatus = vegapb.DataSourceSpec_Status
   203  
   204  type SpecPropertyKey struct {
   205  	Name                string
   206  	Type                SpecPropertyKeyType
   207  	NumberDecimalPlaces *uint64
   208  }
   209  
   210  func (k SpecPropertyKey) String() string {
   211  	var dp string
   212  	if k.NumberDecimalPlaces != nil {
   213  		dp = strconv.FormatUint(*k.NumberDecimalPlaces, 10)
   214  	}
   215  
   216  	return fmt.Sprintf(
   217  		"name(%s) type(%s) decimals(%s)",
   218  		k.Name,
   219  		k.Type.String(),
   220  		dp,
   221  	)
   222  }
   223  
   224  func (k SpecPropertyKey) IntoProto() *datapb.PropertyKey {
   225  	pk := &datapb.PropertyKey{
   226  		Name:                k.Name,
   227  		Type:                k.Type,
   228  		NumberDecimalPlaces: k.NumberDecimalPlaces,
   229  	}
   230  
   231  	return pk
   232  }
   233  
   234  func (k *SpecPropertyKey) DeepClone() *SpecPropertyKey {
   235  	c := k
   236  	return c
   237  }
   238  
   239  func SpecPropertyKeyFromProto(protoKey *datapb.PropertyKey) *SpecPropertyKey {
   240  	return &SpecPropertyKey{
   241  		Name:                protoKey.Name,
   242  		Type:                protoKey.Type,
   243  		NumberDecimalPlaces: protoKey.NumberDecimalPlaces,
   244  	}
   245  }
   246  
   247  func SpecPropertyKeyIsEmpty(key *SpecPropertyKey) bool {
   248  	if key == nil {
   249  		return true
   250  	}
   251  
   252  	if key.Name == "" && key.Type == 0 {
   253  		return true
   254  	}
   255  
   256  	return false
   257  }