Skip to content

Commit d0ebb78

Browse files
committed
fixes
1 parent 11832ca commit d0ebb78

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

coderd/audit/diff.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,38 @@ import (
66
)
77

88
// TODO: this might need to be in the database package.
9-
type DiffMap map[string]interface{}
9+
type Map map[string]interface{}
1010

1111
func Empty[T Auditable]() T {
1212
var t T
1313
return t
1414
}
1515

16-
// Diff compares two auditable resources and produces a map
17-
func Diff[T Auditable](left, right T) DiffMap {
16+
// Diff compares two auditable resources and produces a Map of the changed
17+
// values.
18+
func Diff[T Auditable](left, right T) Map {
1819
// Values are equal, return an empty diff.
1920
if reflect.DeepEqual(left, right) {
20-
return DiffMap{}
21+
return Map{}
2122
}
2223

2324
return diffValues(left, right, AuditableResources)
2425
}
2526

26-
func diffValues[T any](left, right T, table Map) DiffMap {
27+
func structName(t reflect.Type) string {
28+
return t.PkgPath() + "." + t.Name()
29+
}
30+
31+
func diffValues[T any](left, right T, table Table) Map {
2732
var (
28-
baseDiff = DiffMap{}
33+
baseDiff = Map{}
2934

3035
leftV = reflect.ValueOf(left)
3136

3237
rightV = reflect.ValueOf(right)
3338
rightT = reflect.TypeOf(right)
3439

35-
diffKey = table[rightT.Name()]
40+
diffKey = table[structName(rightT)]
3641
)
3742

3843
if diffKey == nil {

coderd/audit/diff_internal_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ func Test_diffValues(t *testing.T) {
2929
{
3030
name: "LeftEmpty",
3131
left: foo{Bar: "", Baz: 0}, right: foo{Bar: "bar", Baz: 10},
32-
exp: DiffMap{
32+
exp: Map{
3333
"bar": "bar",
3434
"baz": int64(10),
3535
},
3636
},
3737
{
3838
name: "RightEmpty",
3939
left: foo{Bar: "Bar", Baz: 10}, right: foo{Bar: "", Baz: 0},
40-
exp: DiffMap{
40+
exp: Map{
4141
"bar": "",
4242
"baz": int64(0),
4343
},
4444
},
4545
{
4646
name: "NoChange",
4747
left: foo{Bar: "", Baz: 0}, right: foo{Bar: "", Baz: 0},
48-
exp: DiffMap{},
48+
exp: Map{},
4949
},
5050
{
5151
name: "SingleFieldChange",
5252
left: foo{Bar: "", Baz: 0}, right: foo{Bar: "Bar", Baz: 0},
53-
exp: DiffMap{
53+
exp: Map{
5454
"bar": "Bar",
5555
},
5656
},
@@ -74,12 +74,12 @@ func Test_diffValues(t *testing.T) {
7474
{
7575
name: "LeftNil",
7676
left: foo{Bar: nil}, right: foo{Bar: pointer.StringPtr("baz")},
77-
exp: DiffMap{"bar": "baz"},
77+
exp: Map{"bar": "baz"},
7878
},
7979
{
8080
name: "RightNil",
8181
left: foo{Bar: pointer.StringPtr("baz")}, right: foo{Bar: nil},
82-
exp: DiffMap{"bar": ""},
82+
exp: Map{"bar": ""},
8383
},
8484
})
8585
})
@@ -108,33 +108,33 @@ func Test_diffValues(t *testing.T) {
108108
{
109109
name: "LeftEmpty",
110110
left: foo{Bar: &bar{}}, right: foo{Bar: &bar{Baz: "baz"}},
111-
exp: DiffMap{
112-
"bar": DiffMap{
111+
exp: Map{
112+
"bar": Map{
113113
"baz": "baz",
114114
},
115115
},
116116
},
117117
{
118118
name: "RightEmpty",
119119
left: foo{Bar: &bar{Baz: "baz"}}, right: foo{Bar: &bar{}},
120-
exp: DiffMap{
121-
"bar": DiffMap{
120+
exp: Map{
121+
"bar": Map{
122122
"baz": "",
123123
},
124124
},
125125
},
126126
{
127127
name: "LeftNil",
128128
left: foo{Bar: nil}, right: foo{Bar: &bar{}},
129-
exp: DiffMap{
130-
"bar": DiffMap{},
129+
exp: Map{
130+
"bar": Map{},
131131
},
132132
},
133133
{
134134
name: "RightNil",
135135
left: foo{Bar: &bar{Baz: "baz"}}, right: foo{Bar: nil},
136-
exp: DiffMap{
137-
"bar": DiffMap{
136+
exp: Map{
137+
"bar": Map{
138138
"baz": "",
139139
},
140140
},
@@ -149,7 +149,7 @@ type diffTest struct {
149149
exp any
150150
}
151151

152-
func runDiffTests(t *testing.T, table Map, tests []diffTest) {
152+
func runDiffTests(t *testing.T, table Table, tests []diffTest) {
153153
t.Helper()
154154

155155
for _, test := range tests {

coderd/audit/diff_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,22 @@ func TestDiff(t *testing.T) {
1818
runDiffTests(t, []diffTest[database.User]{
1919
{
2020
name: "LeftEmpty",
21-
left: audit.Empty[database.User](), right: database.User{Name: "colin", Email: "colin@coder.com"},
22-
exp: audit.DiffMap{
23-
"name": "colin",
21+
left: audit.Empty[database.User](), right: database.User{Username: "colin", Email: "colin@coder.com"},
22+
exp: audit.Map{
2423
"email": "colin@coder.com",
2524
},
2625
},
2726
{
2827
name: "RightEmpty",
29-
left: database.User{Name: "colin", Email: "colin@coder.com"}, right: audit.Empty[database.User](),
30-
exp: audit.DiffMap{
31-
"name": "",
28+
left: database.User{Username: "colin", Email: "colin@coder.com"}, right: audit.Empty[database.User](),
29+
exp: audit.Map{
3230
"email": "",
3331
},
3432
},
3533
{
3634
name: "NoChange",
3735
left: audit.Empty[database.User](), right: audit.Empty[database.User](),
38-
exp: audit.DiffMap{},
36+
exp: audit.Map{},
3937
},
4038
})
4139
})
@@ -44,7 +42,7 @@ func TestDiff(t *testing.T) {
4442
type diffTest[T audit.Auditable] struct {
4543
name string
4644
left, right T
47-
exp audit.DiffMap
45+
exp audit.Map
4846
}
4947

5048
func runDiffTests[T audit.Auditable](t *testing.T, tests []diffTest[T]) {

coderd/audit/table.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,20 @@ const (
2727
ActionSecret = "secret"
2828
)
2929

30-
// Map is a map of struct names to a map of field names that indicate that
30+
// Table is a map of struct names to a map of field names that indicate that
3131
// field's AuditType.
32-
type Map map[string]map[string]Action
32+
type Table map[string]map[string]Action
3333

3434
// AuditableResources contains a definitive list of all auditable resources and
3535
// which fields are auditable.
3636
var AuditableResources = auditMap(map[any]map[string]Action{
3737
&database.User{}: {
3838
"id": ActionIgnore, // Never changes.
3939
"email": ActionTrack, // A user can edit their email.
40-
"name": ActionTrack, // A user can edit their name.
41-
"revoked": ActionTrack, // An admin can revoke a user. This is different from deletion, which is implicit.
42-
"login_type": ActionTrack, // An admin can update the login type of a user.
40+
"username": ActionIgnore, // A user cannot change their username.
4341
"hashed_password": ActionSecret, // A user can change their own password.
4442
"created_at": ActionIgnore, // Never changes.
4543
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
46-
"username": ActionIgnore, // A user cannot change their username.
4744
},
4845
&database.Workspace{}: {
4946
"id": ActionIgnore, // Never changes.
@@ -61,11 +58,11 @@ var AuditableResources = auditMap(map[any]map[string]Action{
6158
// auditMap converts a map of struct pointers to a map of struct names as
6259
// strings. It's a convenience wrapper so that structs can be passed in by value
6360
// instead of manually typing struct names as strings.
64-
func auditMap(m map[any]map[string]Action) Map {
65-
out := make(Map, len(m))
61+
func auditMap(m map[any]map[string]Action) Table {
62+
out := make(Table, len(m))
6663

6764
for k, v := range m {
68-
out[reflect.TypeOf(k).Elem().Name()] = v
65+
out[structName(reflect.TypeOf(k).Elem())] = v
6966
}
7067

7168
return out

0 commit comments

Comments
 (0)