|
| 1 | +package sqlmock |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "database/sql/driver" |
| 6 | + "fmt" |
| 7 | + "reflect" |
| 8 | +) |
| 9 | + |
| 10 | +type namedInOutValue struct { |
| 11 | + Name string |
| 12 | + ExpectedInValue interface{} |
| 13 | + ReturnedOutValue interface{} |
| 14 | + In bool |
| 15 | +} |
| 16 | + |
| 17 | +// Match implements the Argument interface, allowing check if the given value matches the expected input value provided using NamedInputOutputArg function. |
| 18 | +func (n namedInOutValue) Match(v driver.Value) bool { |
| 19 | + out, ok := v.(sql.Out) |
| 20 | + |
| 21 | + return ok && out.In == n.In && (!n.In || reflect.DeepEqual(out.Dest, n.ExpectedInValue)) |
| 22 | +} |
| 23 | + |
| 24 | +// NamedInputArg can ben used to simulate an output value passed back from the database. |
| 25 | +// returnedOutValue can be a value or a pointer to the value. |
| 26 | +func NamedOutputArg(name string, returnedOutValue interface{}) interface{} { |
| 27 | + return namedInOutValue{ |
| 28 | + Name: name, |
| 29 | + ReturnedOutValue: returnedOutValue, |
| 30 | + In: false, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// NamedInputOutputArg can be used to both check if expected input value is provided and to simulate an output value passed back from the database. |
| 35 | +// expectedInValue must be a pointer to the value, returnedOutValue can be a value or a pointer to the value. |
| 36 | +func NamedInputOutputArg(name string, expectedInValue interface{}, returnedOutValue interface{}) interface{} { |
| 37 | + return namedInOutValue{ |
| 38 | + Name: name, |
| 39 | + ExpectedInValue: expectedInValue, |
| 40 | + ReturnedOutValue: returnedOutValue, |
| 41 | + In: true, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +type typedOutValue struct { |
| 46 | + TypeName string |
| 47 | + ReturnedOutValue interface{} |
| 48 | +} |
| 49 | + |
| 50 | +// Match implements the Argument interface, allowing check if the given value matches the expected type provided using TypedOutputArg function. |
| 51 | +func (n typedOutValue) Match(v driver.Value) bool { |
| 52 | + return n.TypeName == fmt.Sprintf("%T", v) |
| 53 | +} |
| 54 | + |
| 55 | +// TypeOutputArg can be used to simulate an output value passed back from the database, setting value based on the type. |
| 56 | +// returnedOutValue must be a pointer to the value. |
| 57 | +func TypedOutputArg(returnedOutValue interface{}) interface{} { |
| 58 | + return typedOutValue{ |
| 59 | + TypeName: fmt.Sprintf("%T", returnedOutValue), |
| 60 | + ReturnedOutValue: returnedOutValue, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func setOutputValues(currentArgs []driver.NamedValue, expectedArgs []driver.Value) { |
| 65 | + for _, expectedArg := range expectedArgs { |
| 66 | + if outVal, ok := expectedArg.(namedInOutValue); ok { |
| 67 | + for _, currentArg := range currentArgs { |
| 68 | + if currentArg.Name == outVal.Name { |
| 69 | + if sqlOut, ok := currentArg.Value.(sql.Out); ok { |
| 70 | + reflect.ValueOf(sqlOut.Dest).Elem().Set(reflect.Indirect(reflect.ValueOf(outVal.ReturnedOutValue))) |
| 71 | + } |
| 72 | + |
| 73 | + break |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if outVal, ok := expectedArg.(typedOutValue); ok { |
| 79 | + for _, currentArg := range currentArgs { |
| 80 | + if fmt.Sprintf("%T", currentArg.Value) == outVal.TypeName { |
| 81 | + reflect.ValueOf(currentArg.Value).Elem().Set(reflect.Indirect(reflect.ValueOf(outVal.ReturnedOutValue))) |
| 82 | + |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments