github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/table/pipeline/row.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pipeline
    16  
    17  import (
    18  	"github.com/dolthub/dolt/go/libraries/doltcore/row"
    19  	"github.com/dolthub/dolt/go/libraries/doltcore/rowconv"
    20  )
    21  
    22  // ReadableMap is an interface that provides read only access to map properties
    23  type ReadableMap interface {
    24  	// Get retrieves an element from the map, and a bool which says if there was a property that exists with that
    25  	// name at all
    26  	Get(propName string) (interface{}, bool)
    27  }
    28  
    29  // NoProps is an empty ImmutableProperties struct
    30  var NoProps = ImmutableProperties{}
    31  
    32  // ImmutableProperties is a map of properties which can't be edited after creation
    33  type ImmutableProperties struct {
    34  	props map[string]interface{}
    35  }
    36  
    37  // Get retrieves an element from the map, and a bool which says if there was a property that exists with that name at all
    38  func (ip ImmutableProperties) Get(propName string) (interface{}, bool) {
    39  	if ip.props == nil {
    40  		return nil, false
    41  	}
    42  
    43  	val, ok := ip.props[propName]
    44  	return val, ok
    45  }
    46  
    47  // Set will create a new ImmutableProperties struct whose values are the original properties combined with the provided
    48  // updates
    49  func (ip ImmutableProperties) Set(updates map[string]interface{}) ImmutableProperties {
    50  	numProps := len(updates) + len(ip.props)
    51  	allProps := make(map[string]interface{}, numProps)
    52  
    53  	for k, v := range ip.props {
    54  		allProps[k] = v
    55  	}
    56  
    57  	for k, v := range updates {
    58  		allProps[k] = v
    59  	}
    60  
    61  	return ImmutableProperties{allProps}
    62  }
    63  
    64  // RowWithProps is a struct that couples a row being processed by a pipeline with properties.  These properties work as
    65  // a means of passing data about a row between stages in a pipeline.
    66  type RowWithProps struct {
    67  	Row   row.Row
    68  	Props ImmutableProperties
    69  }
    70  
    71  // NewRowWithProps creates a RowWith props from a row and a map of properties
    72  func NewRowWithProps(r row.Row, props map[string]interface{}) RowWithProps {
    73  	return RowWithProps{r, ImmutableProperties{props}}
    74  }
    75  
    76  // GetRowConvTranformFunc can be used to wrap a RowConverter and use that RowConverter in a pipeline.
    77  func GetRowConvTransformFunc(rc *rowconv.RowConverter) func(row.Row, ReadableMap) ([]*TransformedRowResult, string) {
    78  	if rc.IdentityConverter {
    79  		return func(inRow row.Row, props ReadableMap) (outRows []*TransformedRowResult, badRowDetails string) {
    80  			return []*TransformedRowResult{{RowData: inRow, PropertyUpdates: nil}}, ""
    81  		}
    82  	} else {
    83  		return func(inRow row.Row, props ReadableMap) (outRows []*TransformedRowResult, badRowDetails string) {
    84  			outRow, err := rc.Convert(inRow)
    85  
    86  			if err != nil {
    87  				return nil, err.Error()
    88  			}
    89  
    90  			if isv, err := row.IsValid(outRow, rc.DestSch); err != nil {
    91  				return nil, err.Error()
    92  			} else if !isv {
    93  				col, err := row.GetInvalidCol(outRow, rc.DestSch)
    94  
    95  				if err != nil {
    96  					return nil, "invalid column"
    97  				} else {
    98  					return nil, "invalid column: " + col.Name
    99  				}
   100  			}
   101  
   102  			return []*TransformedRowResult{{RowData: outRow, PropertyUpdates: nil}}, ""
   103  		}
   104  	}
   105  }