1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package try contains the executable part of the gotry command.
6 // It is not intended for general use.
17 var output io.Writer = os.Stdout // redirected when testing
19 // Main is called directly from the gotry-generated Go source file to perform
21 func Main(pkg, firstArg string, functions map[string]interface{}, args []interface{}) {
26 // Compiler has already evaluated the expression; just print the result.
27 printSlice(firstArg, args)
29 // See if methods satisfy the expressions.
30 tryMethods(pkg, firstArg, args)
31 // See if functions satisfy the expressions.
32 for name, fn := range functions {
33 tryFunction(pkg, name, fn, args)
38 // printSlice prints the zeroth element of the args slice, which should (by construction)
39 // itself be a slice of interface{}.
40 func printSlice(firstArg string, args []interface{}) {
41 // Args should be length 1 and a slice.
45 arg, ok := args[0].([]interface{})
49 fmt.Fprintf(output, "%s = ", firstArg)
51 fmt.Fprint(output, "(")
53 for i, a := range arg {
55 fmt.Fprint(output, ", ")
57 fmt.Fprintf(output, "%#v", a)
60 fmt.Fprint(output, ")")
62 fmt.Fprint(output, "\n")
65 // tryMethods sees if the zeroth arg has methods, and if so treats them as potential
66 // functions to satisfy the remaining arguments.
67 func tryMethods(pkg, firstArg string, args []interface{}) {
68 defer func() { recover() }()
69 // Is the first argument something with methods?
70 v := reflect.NewValue(args[0])
72 if typ.NumMethod() == 0 {
75 for i := 0; i < typ.NumMethod(); i++ {
76 if unicode.IsUpper(int(typ.Method(i).Name[0])) {
77 tryMethod(pkg, firstArg, typ.Method(i), args)
82 // tryMethod converts a method to a function for tryOneFunction.
83 func tryMethod(pkg, firstArg string, method reflect.Method, args []interface{}) {
87 tryOneFunction(pkg, firstArg, name, typ, rfn, args)
90 // tryFunction sees if fn satisfies the arguments.
91 func tryFunction(pkg, name string, fn interface{}, args []interface{}) {
92 defer func() { recover() }()
93 rfn := reflect.NewValue(fn).(*reflect.FuncValue)
94 typ := rfn.Type().(*reflect.FuncType)
95 tryOneFunction(pkg, "", name, typ, rfn, args)
98 // tryOneFunction is the common code for tryMethod and tryFunction.
99 func tryOneFunction(pkg, firstArg, name string, typ *reflect.FuncType, rfn *reflect.FuncValue, args []interface{}) {
101 if typ.NumOut() == 0 {
102 return // Nothing to do.
104 // Right number of arguments + results?
105 if typ.NumIn()+typ.NumOut() != len(args) {
108 // Right argument and result types?
109 for i, a := range args {
111 if !compatible(a, typ.In(i)) {
115 if !compatible(a, typ.Out(i-typ.NumIn())) {
120 // Build the call args.
121 argsVal := make([]reflect.Value, typ.NumIn()+typ.NumOut())
122 for i, a := range args {
123 argsVal[i] = reflect.NewValue(a)
125 // Call the function and see if the results are as expected.
126 resultVal := rfn.Call(argsVal[:typ.NumIn()])
127 for i, v := range resultVal {
128 if !reflect.DeepEqual(v.Interface(), args[i+typ.NumIn()]) {
132 // Present the result including a godoc command to get more information.
135 fmt.Fprintf(output, "%s.%s(", firstArg, name)
138 fmt.Fprintf(output, "%s.%s(", pkg, name)
140 for i := firstIndex; i < typ.NumIn(); i++ {
142 fmt.Fprint(output, ", ")
144 fmt.Fprintf(output, "%#v", args[i])
146 fmt.Fprint(output, ") = ")
147 if typ.NumOut() > 1 {
148 fmt.Fprint(output, "(")
150 for i := 0; i < typ.NumOut(); i++ {
152 fmt.Fprint(output, ", ")
154 fmt.Fprintf(output, "%#v", resultVal[i].Interface())
156 if typ.NumOut() > 1 {
157 fmt.Fprint(output, ")")
159 fmt.Fprintf(output, " // godoc %s %s\n", pkg, name)
162 // compatible reports whether the argument is compatible with the type.
163 func compatible(arg interface{}, typ reflect.Type) bool {
164 if reflect.Typeof(arg) == typ {
168 // nil is OK if the type is an interface.
169 if _, ok := typ.(*reflect.InterfaceType); ok {