Skip to content

Commit f6cda0c

Browse files
committed
Cleaning up api
1 parent 9929e4d commit f6cda0c

14 files changed

+263
-185
lines changed

diff/diff.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ func Diff(lhs, rhs interface{}) (Differ, error) {
2424
rhsVal := reflect.ValueOf(rhs)
2525

2626
if lhs == nil && rhs == nil {
27-
return &Scalar{lhs, rhs}, nil
27+
return scalar{lhs, rhs}, nil
2828
}
2929
if lhs == nil || rhs == nil {
30-
return &Types{lhs, rhs}, nil
30+
return types{lhs, rhs}, nil
3131
}
3232
if lhsVal.Type().Comparable() && rhsVal.Type().Comparable() {
33-
return &Scalar{lhs, rhs}, nil
33+
return scalar{lhs, rhs}, nil
3434
}
3535
if lhsVal.Kind() != rhsVal.Kind() {
36-
return &Types{lhs, rhs}, nil
36+
return types{lhs, rhs}, nil
3737
}
3838
if lhsVal.Kind() == reflect.Slice {
39-
return NewSlice(lhs, rhs)
39+
return newSlice(lhs, rhs)
4040
}
4141
if lhsVal.Kind() == reflect.Map {
42-
return NewMap(lhs, rhs)
42+
return newMap(lhs, rhs)
4343
}
4444

45-
return &Types{lhs, rhs}, &Unsupported{lhsVal.Type(), rhsVal.Type()}
45+
return types{lhs, rhs}, &ErrUnsupported{lhsVal.Type(), rhsVal.Type()}
4646
}
4747

4848
func (t Type) String() string {
@@ -57,3 +57,27 @@ func (t Type) String() string {
5757

5858
return "invalid type"
5959
}
60+
61+
// IsExcess returns true if the provided Differ refers to a value missing in LHS
62+
func IsExcess(d Differ) bool {
63+
switch d.(type) {
64+
default:
65+
return false
66+
case mapExcess:
67+
return true
68+
case sliceExcess:
69+
return true
70+
}
71+
}
72+
73+
// IsMissing returns true if the provided Differ refers to a value missing in LHS
74+
func IsMissing(d Differ) bool {
75+
switch d.(type) {
76+
default:
77+
return false
78+
case mapMissing:
79+
return true
80+
case sliceMissing:
81+
return true
82+
}
83+
}

diff/diff_test.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestTypes(t *testing.T) {
118118
},
119119
},
120120
} {
121-
typ := &Types{test.LHS, test.RHS}
121+
typ := types{test.LHS, test.RHS}
122122

123123
if typ.Diff() != TypesDiffer {
124124
t.Errorf("Types.Diff() = %q, expected %q", typ.Diff(), TypesDiffer)
@@ -159,7 +159,7 @@ func TestScalar(t *testing.T) {
159159
Type: TypesDiffer,
160160
},
161161
} {
162-
typ := &Scalar{test.LHS, test.RHS}
162+
typ := scalar{test.LHS, test.RHS}
163163

164164
if typ.Diff() != test.Type {
165165
t.Errorf("Types.Diff() = %q, expected %q", typ.Diff(), test.Type)
@@ -223,7 +223,7 @@ func TestSlice(t *testing.T) {
223223
Type: ContentDiffer,
224224
},
225225
} {
226-
typ, err := NewSlice(test.LHS, test.RHS)
226+
typ, err := newSlice(test.LHS, test.RHS)
227227

228228
if err != nil {
229229
t.Errorf("NewSlice(%+v, %+v): unexpected error: %q", test.LHS, test.RHS, err)
@@ -238,8 +238,8 @@ func TestSlice(t *testing.T) {
238238
testStrings("TestSlice", t, test, ss, indented)
239239
}
240240

241-
invalid, err := NewSlice(nil, nil)
242-
if invalidErr, ok := err.(InvalidType); ok {
241+
invalid, err := newSlice(nil, nil)
242+
if invalidErr, ok := err.(ErrInvalidType); ok {
243243
if !strings.Contains(invalidErr.Error(), "nil") {
244244
t.Errorf("NewSlice(nil, nil): unexpected format for InvalidType error: got %s", err)
245245
}
@@ -256,8 +256,8 @@ func TestSlice(t *testing.T) {
256256
t.Errorf("invalidSlice.StringIndent(%q, %q, %+v) = %q, expected %q", testKey, testPrefix, testOutput, indented, "")
257257
}
258258

259-
invalid, err = NewSlice([]int{}, nil)
260-
if invalidErr, ok := err.(InvalidType); ok {
259+
invalid, err = newSlice([]int{}, nil)
260+
if invalidErr, ok := err.(ErrInvalidType); ok {
261261
if !strings.Contains(invalidErr.Error(), "nil") {
262262
t.Errorf("NewSlice([]int{}, nil): unexpected format for InvalidType error: got %s", err)
263263
}
@@ -338,7 +338,7 @@ func TestMap(t *testing.T) {
338338
Type: ContentDiffer,
339339
},
340340
} {
341-
m, err := NewMap(test.LHS, test.RHS)
341+
m, err := newMap(test.LHS, test.RHS)
342342

343343
if err != nil {
344344
t.Errorf("NewMap(%+v, %+v): unexpected error: %q", test.LHS, test.RHS, err)
@@ -353,8 +353,8 @@ func TestMap(t *testing.T) {
353353
testStrings(fmt.Sprintf("TestMap[%d]", i), t, test, ss, indented)
354354
}
355355

356-
invalid, err := NewMap(nil, nil)
357-
if invalidErr, ok := err.(InvalidType); ok {
356+
invalid, err := newMap(nil, nil)
357+
if invalidErr, ok := err.(ErrInvalidType); ok {
358358
if !strings.Contains(invalidErr.Error(), "nil") {
359359
t.Errorf("NewMap(nil, nil): unexpected format for InvalidType error: got %s", err)
360360
}
@@ -371,8 +371,8 @@ func TestMap(t *testing.T) {
371371
t.Errorf("invalidMap.StringIndent(%q, %q, %+v) = %q, expected %q", testKey, testPrefix, testOutput, indented, "")
372372
}
373373

374-
invalid, err = NewMap(map[int]int{}, nil)
375-
if invalidErr, ok := err.(InvalidType); ok {
374+
invalid, err = newMap(map[int]int{}, nil)
375+
if invalidErr, ok := err.(ErrInvalidType); ok {
376376
if !strings.Contains(invalidErr.Error(), "nil") {
377377
t.Errorf("NewMap(map[int]int{}, nil): unexpected format for InvalidType error: got %s", err)
378378
}
@@ -391,16 +391,16 @@ func TestMap(t *testing.T) {
391391
}
392392

393393
func TestIgnore(t *testing.T) {
394-
ignore := Ignore{}
394+
ignoreDiff, _ := Ignore()
395395

396-
if ignore.Diff() != Identical {
397-
t.Errorf("Ignore{}.Diff() = %q, expected %q", ignore.Diff(), Identical)
396+
if ignoreDiff.Diff() != Identical {
397+
t.Errorf("NewIgnore().Diff() = %q, expected %q", ignoreDiff.Diff(), Identical)
398398
}
399-
if len(ignore.Strings()) != 0 {
400-
t.Errorf("len(Ignore{}.Strings()) = %d, expected 0", len(ignore.Strings()))
399+
if len(ignoreDiff.Strings()) != 0 {
400+
t.Errorf("len(NewIgnore().Strings()) = %d, expected 0", len(ignoreDiff.Strings()))
401401
}
402-
if indented := ignore.StringIndent(testKey, testPrefix, testOutput); indented != "" {
403-
t.Errorf("Ignore{}.StringIndent(...) = %q, expected %q", indented, "")
402+
if indented := ignoreDiff.StringIndent(testKey, testPrefix, testOutput); indented != "" {
403+
t.Errorf("NewIgnore().StringIndent(...) = %q, expected %q", indented, "")
404404
}
405405
}
406406

diff/errors.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import (
55
"reflect"
66
)
77

8-
type Unsupported struct {
8+
type ErrUnsupported struct {
99
LHS reflect.Type
1010
RHS reflect.Type
1111
}
1212

13-
func (e Unsupported) Error() string {
13+
func (e ErrUnsupported) Error() string {
1414
return "unsupported types: " + e.LHS.String() + ", " + e.RHS.String()
1515
}
1616

17-
type InvalidType struct {
17+
type ErrInvalidType struct {
1818
Value interface{}
1919
For string
2020
}
2121

22-
func (e InvalidType) Error() string {
22+
func (e ErrInvalidType) Error() string {
2323
return fmt.Sprintf("%T is not a valid type for %s", e.Value, e.For)
2424
}

diff/errors_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestUnsupported(t *testing.T) {
1515
{LHS: reflect.TypeOf(func() {}), RHS: reflect.TypeOf(0), Want: "func()"},
1616
{LHS: reflect.TypeOf(0), RHS: reflect.TypeOf(struct{}{}), Want: "struct"},
1717
} {
18-
s := Unsupported{test.LHS, test.RHS}.Error()
18+
s := ErrUnsupported{test.LHS, test.RHS}.Error()
1919

2020
if !strings.Contains(s, test.Want) {
2121
t.Errorf("Unsupported.Error() = %q, expected it to contain %q", s, test.Want)

diff/ignore.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package diff
22

3-
type Ignore struct{}
3+
type ignore struct{}
44

5-
func (t Ignore) Diff() Type {
5+
func Ignore() (Differ, error) {
6+
return ignore{}, nil
7+
}
8+
9+
func IsIgnore(d Differ) bool {
10+
_, ok := d.(ignore)
11+
12+
return ok
13+
}
14+
15+
func (t ignore) Diff() Type {
616
return Identical
717
}
818

9-
func (t Ignore) Strings() []string {
19+
func (t ignore) Strings() []string {
1020
return []string{}
1121
}
1222

13-
func (t Ignore) StringIndent(key, prefix string, conf Output) string {
23+
func (t ignore) StringIndent(key, prefix string, conf Output) string {
1424
return ""
1525
}

0 commit comments

Comments
 (0)