Skip to content

chore: join owner, template, and org in new workspace view #15116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "remove unused"
This reverts commit e23704c.
  • Loading branch information
Emyrk committed Oct 22, 2024
commit ba8077dacd5b170c0717b3de8f19495954ddf04b
19 changes: 19 additions & 0 deletions coderd/database/modelqueries_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package database

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/testutil"
)

func TestIsAuthorizedQuery(t *testing.T) {
Expand All @@ -13,3 +16,19 @@ func TestIsAuthorizedQuery(t *testing.T) {
_, err := insertAuthorizedFilter(query, "")
require.ErrorContains(t, err, "does not contain authorized replace string", "ensure replace string")
}

// TestWorkspaceTableConvert verifies all workspace fields are converted
// when reducing a `Workspace` to a `WorkspaceTable`.
func TestWorkspaceTableConvert(t *testing.T) {
t.Parallel()

var workspace Workspace
err := testutil.PopulateStruct(&workspace, nil)
require.NoError(t, err)

workspace.WorkspaceTable()
require.JSONEq(t)

fmt.Println(workspace)

}
139 changes: 139 additions & 0 deletions testutil/reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package testutil

import (
"fmt"
"reflect"
"time"
)

type Random struct {
String func() string
Bool func() bool
Int func() int64
Uint func() uint64
Float func() float64
Complex func() complex128
}

func NewRandom() *Random {
// Guaranteed to be random...
return &Random{
String: func() string { return "foo" },
Bool: func() bool { return true },
Int: func() int64 { return 500 },
Uint: func() uint64 { return 126 },
Float: func() float64 { return 3.14 },
Complex: func() complex128 { return 6.24 },
}
}

// PopulateStruct does a best effort to populate a struct with random values.
func PopulateStruct(s interface{}, r *Random) error {
if r == nil {
r = NewRandom()
}

v := reflect.ValueOf(s)
if v.Kind() != reflect.Ptr || v.IsNil() {
return fmt.Errorf("s must be a non-nil pointer")
}

v = v.Elem()
if v.Kind() != reflect.Struct {
return fmt.Errorf("s must be a pointer to a struct")
}

t := v.Type()
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fieldName := field.Name

fieldValue := v.Field(i)
if !fieldValue.CanSet() {
continue // Skip if field is unexported
}

nv, err := populateValue(fieldValue, r)
if err != nil {
return fmt.Errorf("%s : %w", fieldName, err)
}
v.Field(i).Set(nv)
}

return nil
}

func populateValue(v reflect.Value, r *Random) (reflect.Value, error) {
var err error

// Handle some special cases
switch v.Type() {
case reflect.TypeOf(time.Time{}):
v.Set(reflect.ValueOf(time.Date(2020, 5, 2, 5, 19, 21, 30, time.UTC)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: use a non-UTC timezone

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to be lazy here.

return v, nil
}

switch v.Kind() {
case reflect.Struct:
if err := PopulateStruct(v.Addr().Interface(), r); err != nil {
return v, err
}
case reflect.String:
v.SetString(r.String())
case reflect.Bool:
v.SetBool(true)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v.SetInt(r.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v.SetUint(r.Uint())
case reflect.Float32, reflect.Float64:
v.SetFloat(r.Float())
case reflect.Complex64, reflect.Complex128:
v.SetComplex(r.Complex())
case reflect.Array:
for i := 0; i < v.Len(); i++ {
nv, err := populateValue(v.Index(i), r)
if err != nil {
return v, fmt.Errorf("array index %d : %w", i, err)
}
v.Index(i).Set(nv)
}
case reflect.Map:
m := reflect.MakeMap(v.Type())

// Set a value in the map
k := reflect.New(v.Type().Key())
kv := reflect.New(v.Type().Elem())
k, err = populateValue(k, r)
if err != nil {
return v, fmt.Errorf("map key : %w", err)
}
kv, err = populateValue(kv, r)
if err != nil {
return v, fmt.Errorf("map value : %w", err)
}

m.SetMapIndex(k, kv)
return m, nil
case reflect.Pointer:
return populateValue(v.Elem(), r)
case reflect.Slice:
s := reflect.MakeSlice(v.Type(), 2, 2)
sv, err := populateValue(reflect.New(v.Type().Elem()), r)
if err != nil {
return v, fmt.Errorf("slice value : %w", err)
}

s.Index(0).Set(sv)
s.Index(1).Set(sv)
//reflect.AppendSlice(s, sv)

return s, nil
case reflect.Uintptr, reflect.UnsafePointer, reflect.Chan, reflect.Func, reflect.Interface:
// Unsupported
return v, fmt.Errorf("%s is not supported", v.Kind())
default:
return v, fmt.Errorf("unsupported kind %s", v.Kind())
}
return v, nil
}