wire: Add wire.InterfaceValue, required instead of wire.Value if the value is an interface value (google/go-cloud#322)

* Add wire.InterfaceValue, required instead of wire.Value if the value is an interface value.

* Update guestbook sample to use InterfaceValue where appropriate.

* Remove unnecessary ok := true

* Addressing comments from code review.
This commit is contained in:
Robert van Gent
2018-08-14 14:55:28 -07:00
committed by Ross Light
parent eedae3d8d0
commit cd32a686b1
20 changed files with 296 additions and 3 deletions

View File

@@ -0,0 +1,26 @@
// Copyright 2018 The Go Cloud Authors
//
// 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
//
// 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 main
import (
"fmt"
"io/ioutil"
)
func main() {
r := injectedReader()
buf, _ := ioutil.ReadAll(r)
fmt.Println(string(buf))
}

View File

@@ -0,0 +1,29 @@
// Copyright 2018 The Go Cloud Authors
//
// 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
//
// 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.
//+build wireinject
package main
import (
"io"
"strings"
"github.com/google/go-cloud/wire"
)
func injectedReader() io.Reader {
wire.Build(wire.InterfaceValue(new(io.Reader), strings.NewReader("hello world")))
return nil
}

View File

@@ -0,0 +1 @@
foo

View File

@@ -0,0 +1 @@
hello world

View File

@@ -0,0 +1,22 @@
// Code generated by Wire. DO NOT EDIT.
//go:generate wire
//+build !wireinject
package main
import (
io "io"
strings "strings"
)
// Injectors from wire.go:
func injectedReader() io.Reader {
reader := _wireReaderValue
return reader
}
var (
_wireReaderValue = strings.NewReader("hello world")
)

View File

@@ -0,0 +1,21 @@
// Copyright 2018 The Go Cloud Authors
//
// 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
//
// 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 main
import "fmt"
func main() {
fmt.Println(injectedMessage())
}

View File

@@ -0,0 +1,26 @@
// Copyright 2018 The Go Cloud Authors
//
// 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
//
// 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.
//+build wireinject
package main
import (
"github.com/google/go-cloud/wire"
)
func injectedMessage() string {
wire.Build(wire.InterfaceValue("foo", "bar"))
return ""
}

View File

@@ -0,0 +1 @@
foo

View File

@@ -0,0 +1 @@
first argument to InterfaceValue must be a pointer to an interface type; found string

View File

@@ -0,0 +1,21 @@
// Copyright 2018 The Go Cloud Authors
//
// 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
//
// 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 main
import "fmt"
func main() {
fmt.Println(injectedMessage())
}

View File

@@ -0,0 +1,26 @@
// Copyright 2018 The Go Cloud Authors
//
// 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
//
// 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.
//+build wireinject
package main
import (
"github.com/google/go-cloud/wire"
)
func injectedMessage() string {
wire.Build(wire.InterfaceValue("foo"))
return ""
}

View File

@@ -0,0 +1 @@
foo

View File

@@ -0,0 +1 @@
too few arguments in call to wire.InterfaceValue

View File

@@ -0,0 +1,27 @@
// Copyright 2018 The Go Cloud Authors
//
// 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
//
// 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 main
import (
"fmt"
"io/ioutil"
"strings"
)
func main() {
r := injectedReader(strings.NewReader("hello world"))
buf, _ := ioutil.ReadAll(r)
fmt.Println(string(buf))
}

View File

@@ -0,0 +1,29 @@
// Copyright 2018 The Go Cloud Authors
//
// 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
//
// 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.
//+build wireinject
package main
import (
"io"
"strings"
"github.com/google/go-cloud/wire"
)
func injectedReader(r *strings.Reader) io.Reader {
wire.Build(wire.Value(io.Reader(r)))
return nil
}

View File

@@ -0,0 +1 @@
foo

View File

@@ -0,0 +1 @@
argument to Value may not be an interface value (found io.Reader); use InterfaceValue instead