Skip to content
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

Add reflection.Type[T]() helper and use it. #370

Merged
merged 3 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 3 additions & 1 deletion fillrefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package weaver
import (
"fmt"
"reflect"

"github.com/ServiceWeaver/weaver/internal/reflection"
)

// fillRefs initializes Ref[T] fields in a component implement struct.
Expand All @@ -32,7 +34,7 @@ func fillRefs(impl any, get func(reflect.Type) (any, error)) error {
if s.Kind() != reflect.Struct {
return fmt.Errorf("not a struct pointer")
}
isRef := reflect.TypeOf((*interface{ isRef() })(nil)).Elem()
isRef := reflection.Type[interface{ isRef() }]()
for i, n := 0, s.NumField(); i < n; i++ {
// Handle field with type weaver.Ref[T].
ref := s.Field(i)
Expand Down
4 changes: 4 additions & 0 deletions godeps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ github.com/ServiceWeaver/weaver
github.com/ServiceWeaver/weaver/internal/metrics
github.com/ServiceWeaver/weaver/internal/net/call
github.com/ServiceWeaver/weaver/internal/private
github.com/ServiceWeaver/weaver/internal/reflection
github.com/ServiceWeaver/weaver/internal/register
github.com/ServiceWeaver/weaver/internal/status
github.com/ServiceWeaver/weaver/internal/tool/single
Expand Down Expand Up @@ -466,6 +467,8 @@ github.com/ServiceWeaver/weaver/internal/queue
context
github.com/ServiceWeaver/weaver/internal/cond
sync
github.com/ServiceWeaver/weaver/internal/reflection
reflect
github.com/ServiceWeaver/weaver/internal/register
fmt
sync
Expand Down Expand Up @@ -901,6 +904,7 @@ github.com/ServiceWeaver/weaver/weavertest
github.com/ServiceWeaver/weaver
github.com/ServiceWeaver/weaver/internal/envelope/conn
github.com/ServiceWeaver/weaver/internal/private
github.com/ServiceWeaver/weaver/internal/reflection
github.com/ServiceWeaver/weaver/runtime
github.com/ServiceWeaver/weaver/runtime/codegen
github.com/ServiceWeaver/weaver/runtime/envelope
Expand Down
3 changes: 2 additions & 1 deletion internal/envelope/conn/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/ServiceWeaver/weaver"
"github.com/ServiceWeaver/weaver/internal/envelope/conn"
"github.com/ServiceWeaver/weaver/internal/reflection"
"github.com/ServiceWeaver/weaver/metrics"
"github.com/ServiceWeaver/weaver/runtime/codegen"
"github.com/ServiceWeaver/weaver/runtime/protos"
Expand Down Expand Up @@ -195,7 +196,7 @@ func register[Intf, Impl any](name string) {
var zero Impl
codegen.Register(codegen.Registration{
Name: name,
Iface: reflect.TypeOf((*Intf)(nil)).Elem(),
Iface: reflection.Type[Intf](),
Impl: reflect.TypeOf(zero),
LocalStubFn: func(any, trace.Tracer) any { return nil },
ClientStubFn: func(codegen.Stub, string) any { return nil },
Expand Down
26 changes: 26 additions & 0 deletions internal/reflection/reflection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package reflection implements helpers for reflection code.
package reflection

import "reflect"

// Type returns the reflect.Type for T.
//
// This function is particularly useful when T is an interface
// and it is impossible to get a value with concrete type T.
func Type[T any]() reflect.Type {
return reflect.TypeOf((*T)(nil)).Elem()
}
40 changes: 40 additions & 0 deletions internal/reflection/reflection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package reflection_test

import (
"fmt"
"io"
"reflect"
"testing"

"github.com/ServiceWeaver/weaver/internal/reflection"
)

func TestType(t *testing.T) {
for _, test := range []struct {
want string
t reflect.Type
}{
{"int", reflection.Type[int]()},
{"io.Reader", reflection.Type[io.Reader]()},
} {
t.Run(test.want, func(t *testing.T) {
if got := fmt.Sprint(test.t); got != test.want {
t.Errorf("reflection.Type[io.Reader] = %s, expecting %s", got, test.want)
ghemawat marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
}
3 changes: 2 additions & 1 deletion runtime/codegen/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/ServiceWeaver/weaver"
"github.com/ServiceWeaver/weaver/internal/reflection"
"github.com/ServiceWeaver/weaver/runtime/codegen"
"go.opentelemetry.io/otel/trace"
)
Expand Down Expand Up @@ -120,7 +121,7 @@ func register[Intf, Impl any](name string) {
var zero Impl
codegen.Register(codegen.Registration{
Name: name,
Iface: reflect.TypeOf((*Intf)(nil)).Elem(),
Iface: reflection.Type[Intf](),
Impl: reflect.TypeOf(zero),
LocalStubFn: func(any, trace.Tracer) any { return nil },
ClientStubFn: func(codegen.Stub, string) any { return nil },
Expand Down
3 changes: 2 additions & 1 deletion runtime/envelope/envelope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/ServiceWeaver/weaver"
"github.com/ServiceWeaver/weaver/internal/envelope/conn"
"github.com/ServiceWeaver/weaver/internal/reflection"
"github.com/ServiceWeaver/weaver/internal/traceio"
"github.com/ServiceWeaver/weaver/runtime"
"github.com/ServiceWeaver/weaver/runtime/codegen"
Expand Down Expand Up @@ -549,7 +550,7 @@ func register[Intf, Impl any](name string) {
var zero Impl
codegen.Register(codegen.Registration{
Name: name,
Iface: reflect.TypeOf((*Intf)(nil)).Elem(),
Iface: reflection.Type[Intf](),
Impl: reflect.TypeOf(zero),
LocalStubFn: func(any, trace.Tracer) any { return nil },
ClientStubFn: func(codegen.Stub, string) any { return nil },
Expand Down
3 changes: 2 additions & 1 deletion weavelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ServiceWeaver/weaver/internal/envelope/conn"
"github.com/ServiceWeaver/weaver/internal/net/call"
"github.com/ServiceWeaver/weaver/internal/private"
"github.com/ServiceWeaver/weaver/internal/reflection"
"github.com/ServiceWeaver/weaver/internal/traceio"
"github.com/ServiceWeaver/weaver/runtime"
"github.com/ServiceWeaver/weaver/runtime/codegen"
Expand Down Expand Up @@ -250,7 +251,7 @@ func (w *weavelet) start() error {
func (w *weavelet) getMainIfLocal() (*componentImpl, error) {
// Note that a weavertest may have RunMain set to true, but no main
// component registered.
if m, ok := w.componentsByType[reflect.TypeOf((*Main)(nil)).Elem()]; ok && w.info.RunMain {
if m, ok := w.componentsByType[reflection.Type[Main]()]; ok && w.info.RunMain {
return w.getImpl(w.ctx, m)
}
return nil, nil
Expand Down
6 changes: 4 additions & 2 deletions weavertest/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/ServiceWeaver/weaver"
"github.com/ServiceWeaver/weaver/internal/private"
"github.com/ServiceWeaver/weaver/internal/reflection"
"github.com/ServiceWeaver/weaver/runtime/logging"
)

Expand Down Expand Up @@ -83,10 +84,11 @@ type FakeComponent struct {
// The result is typically placed in Runner.Fakes.
// REQUIRES: impl must implement T.
func Fake[T any](impl any) FakeComponent {
t := reflection.Type[T]()
if _, ok := impl.(T); !ok {
panic(fmt.Sprintf("%T does not implement %v", impl, reflect.TypeOf((*T)(nil)).Elem()))
panic(fmt.Sprintf("%T does not implement %v", impl, t))
}
return FakeComponent{intf: reflect.TypeOf((*T)(nil)).Elem(), impl: impl}
return FakeComponent{intf: t, impl: impl}
}

// private.App object per live test or benchmark.
Expand Down