github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/conflict/conflict.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 conflict 16 17 import ( 18 "context" 19 "errors" 20 21 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 22 "github.com/dolthub/dolt/go/libraries/doltcore/schema/encoding" 23 "github.com/dolthub/dolt/go/store/types" 24 ) 25 26 type ConflictSchema struct { 27 Base schema.Schema 28 Schema schema.Schema 29 MergeSchema schema.Schema 30 } 31 32 func NewConflictSchema(base, sch, mergeSch schema.Schema) ConflictSchema { 33 return ConflictSchema{ 34 Base: base, 35 Schema: sch, 36 MergeSchema: mergeSch, 37 } 38 } 39 40 func ValueFromConflictSchema(ctx context.Context, vrw types.ValueReadWriter, cs ConflictSchema) (types.Value, error) { 41 b, err := serializeSchema(ctx, vrw, cs.Base) 42 if err != nil { 43 return nil, err 44 } 45 46 s, err := serializeSchema(ctx, vrw, cs.Schema) 47 if err != nil { 48 return nil, err 49 } 50 51 m, err := serializeSchema(ctx, vrw, cs.MergeSchema) 52 if err != nil { 53 return nil, err 54 } 55 56 return types.NewTuple(vrw.Format(), b, s, m) 57 } 58 59 func ConflictSchemaFromValue(ctx context.Context, vrw types.ValueReadWriter, v types.Value) (cs ConflictSchema, err error) { 60 tup, ok := v.(types.Tuple) 61 if !ok { 62 err = errors.New("conflict schema value must be types.Struct") 63 return ConflictSchema{}, err 64 } 65 66 b, err := tup.Get(0) 67 if err != nil { 68 return ConflictSchema{}, err 69 } 70 cs.Base, err = deserializeSchema(ctx, vrw, b) 71 if err != nil { 72 return ConflictSchema{}, err 73 } 74 75 s, err := tup.Get(1) 76 if err != nil { 77 return ConflictSchema{}, err 78 } 79 cs.Schema, err = deserializeSchema(ctx, vrw, s) 80 if err != nil { 81 return ConflictSchema{}, err 82 } 83 84 m, err := tup.Get(2) 85 if err != nil { 86 return ConflictSchema{}, err 87 } 88 cs.MergeSchema, err = deserializeSchema(ctx, vrw, m) 89 if err != nil { 90 return ConflictSchema{}, err 91 } 92 93 return cs, nil 94 } 95 96 func serializeSchema(ctx context.Context, vrw types.ValueReadWriter, sch schema.Schema) (types.Ref, error) { 97 st, err := encoding.MarshalSchema(ctx, vrw, sch) 98 if err != nil { 99 return types.Ref{}, err 100 } 101 102 return vrw.WriteValue(ctx, st) 103 } 104 105 func deserializeSchema(ctx context.Context, vrw types.ValueReadWriter, v types.Value) (schema.Schema, error) { 106 r, ok := v.(types.Ref) 107 if !ok { 108 return nil, errors.New("conflict schemas field value is unexpected type") 109 } 110 111 return encoding.UnmarshalSchemaAtAddr(ctx, vrw, r.TargetHash()) 112 } 113 114 type Conflict struct { 115 Base types.Value 116 Value types.Value 117 MergeValue types.Value 118 } 119 120 func NewConflict(base, value, mergeValue types.Value) Conflict { 121 if base == nil { 122 base = types.NullValue 123 } 124 if value == nil { 125 value = types.NullValue 126 } 127 if mergeValue == nil { 128 mergeValue = types.NullValue 129 } 130 return Conflict{base, value, mergeValue} 131 } 132 133 func ConflictFromTuple(tpl types.Tuple) (Conflict, error) { 134 base, err := tpl.Get(0) 135 136 if err != nil { 137 return Conflict{}, err 138 } 139 140 val, err := tpl.Get(1) 141 142 if err != nil { 143 return Conflict{}, err 144 } 145 146 mv, err := tpl.Get(2) 147 148 if err != nil { 149 return Conflict{}, err 150 } 151 return Conflict{base, val, mv}, nil 152 } 153 154 func (c Conflict) ToNomsList(vrw types.ValueReadWriter) (types.Tuple, error) { 155 return types.NewTuple(vrw.Format(), c.Base, c.Value, c.MergeValue) 156 }