|
| 1 | +// Copyright (c) 2024 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package fx |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "reflect" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "go.uber.org/fx/internal/fxreflect" |
| 29 | +) |
| 30 | + |
| 31 | +// Evaluate specifies one or more evaluation functions. |
| 32 | +// These are functions that accept dependencies from the graph |
| 33 | +// and return an fx.Option. |
| 34 | +// They may have the following signatures: |
| 35 | +// |
| 36 | +// func(...) fx.Option |
| 37 | +// func(...) (fx.Option, error) |
| 38 | +// |
| 39 | +// These functions are run after provides and decorates. |
| 40 | +// The resulting options are applied to the graph, |
| 41 | +// and may introduce new provides, invokes, decorates, or evaluates. |
| 42 | +// |
| 43 | +// The effect of this is that parts of the graph can be dynamically generated |
| 44 | +// based on dependency values. |
| 45 | +// |
| 46 | +// For example, a function with a dependency on a configuration struct |
| 47 | +// could conditionally provide different implementations based on the value. |
| 48 | +// |
| 49 | +// fx.Evaluate(func(cfg *Config) fx.Option { |
| 50 | +// if cfg.Environment == "production" { |
| 51 | +// return fx.Provide(func(*sql.DB) Repository { |
| 52 | +// return &sqlRepository{db: db} |
| 53 | +// }), |
| 54 | +// } else { |
| 55 | +// return fx.Provide(func() Repository { |
| 56 | +// return &memoryRepository{} |
| 57 | +// }) |
| 58 | +// } |
| 59 | +// }) |
| 60 | +// |
| 61 | +// This is different from a normal provide that inspects the configuration |
| 62 | +// because the dependency on '*sql.DB' is completely absent in the graph |
| 63 | +// if the configuration is not "production". |
| 64 | +func Evaluate(fns ...any) Option { |
| 65 | + return evaluateOption{ |
| 66 | + Targets: fns, |
| 67 | + Stack: fxreflect.CallerStack(1, 0), |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +type evaluateOption struct { |
| 72 | + Targets []any |
| 73 | + Stack fxreflect.Stack |
| 74 | +} |
| 75 | + |
| 76 | +func (o evaluateOption) apply(mod *module) { |
| 77 | + for _, target := range o.Targets { |
| 78 | + mod.evaluates = append(mod.evaluates, evaluate{ |
| 79 | + Target: target, |
| 80 | + Stack: o.Stack, |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func (o evaluateOption) String() string { |
| 86 | + items := make([]string, len(o.Targets)) |
| 87 | + for i, target := range o.Targets { |
| 88 | + items[i] = fxreflect.FuncName(target) |
| 89 | + } |
| 90 | + return fmt.Sprintf("fx.Evaluate(%s)", strings.Join(items, ", ")) |
| 91 | +} |
| 92 | + |
| 93 | +type evaluate struct { |
| 94 | + Target any |
| 95 | + Stack fxreflect.Stack |
| 96 | +} |
| 97 | + |
| 98 | +func runEvaluate(m *module, e evaluate) (err error) { |
| 99 | + target := e.Target |
| 100 | + defer func() { |
| 101 | + if err != nil { |
| 102 | + err = fmt.Errorf("fx.Evaluate(%v) from:\n%+vFailed: %w", target, e.Stack, err) |
| 103 | + } |
| 104 | + }() |
| 105 | + |
| 106 | + // target is a function returning (Option, error). |
| 107 | + // Use reflection to build a function with the same parameters, |
| 108 | + // and invoke that in the container. |
| 109 | + targetV := reflect.ValueOf(target) |
| 110 | + targetT := targetV.Type() |
| 111 | + inTypes := make([]reflect.Type, targetT.NumIn()) |
| 112 | + for i := range targetT.NumIn() { |
| 113 | + inTypes[i] = targetT.In(i) |
| 114 | + } |
| 115 | + outTypes := []reflect.Type{reflect.TypeOf((*error)(nil)).Elem()} |
| 116 | + |
| 117 | + // TODO: better way to extract information from the container |
| 118 | + var opt Option |
| 119 | + invokeFn := reflect.MakeFunc( |
| 120 | + reflect.FuncOf(inTypes, outTypes, false), |
| 121 | + func(args []reflect.Value) []reflect.Value { |
| 122 | + out := targetV.Call(args) |
| 123 | + switch len(out) { |
| 124 | + case 2: |
| 125 | + if err, _ := out[1].Interface().(error); err != nil { |
| 126 | + return []reflect.Value{reflect.ValueOf(err)} |
| 127 | + } |
| 128 | + |
| 129 | + fallthrough |
| 130 | + case 1: |
| 131 | + opt, _ = out[0].Interface().(Option) |
| 132 | + |
| 133 | + default: |
| 134 | + panic("TODO: validation") |
| 135 | + } |
| 136 | + |
| 137 | + return []reflect.Value{ |
| 138 | + reflect.Zero(reflect.TypeOf((*error)(nil)).Elem()), |
| 139 | + } |
| 140 | + }, |
| 141 | + ).Interface() |
| 142 | + if err := m.scope.Invoke(invokeFn); err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + if opt == nil { |
| 147 | + // Assume no-op. |
| 148 | + return nil |
| 149 | + } |
| 150 | + |
| 151 | + opt.apply(m) |
| 152 | + return nil |
| 153 | +} |
0 commit comments