| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package inject |
| 27 |
|
| 28 |
import ( |
| 29 |
"bytes" |
| 30 |
"fmt" |
| 31 |
"math/rand" |
| 32 |
"reflect" |
| 33 |
|
| 34 |
"github.com/facebookgo/structtag" |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
type Logger interface { |
| 40 |
Debugf(format string, v ...interface{}) |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func Populate(values ...interface{}) error { |
| 46 |
var g Graph |
| 47 |
for _, v := range values { |
| 48 |
if err := g.Provide(&Object{Value: v}); err != nil { |
| 49 |
return err |
| 50 |
} |
| 51 |
} |
| 52 |
return g.Populate() |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
type Object struct { |
| 57 |
Value interface{} |
| 58 |
Name string |
| 59 |
Complete bool |
| 60 |
Fields map[string]*Object |
| 61 |
reflectType reflect.Type |
| 62 |
reflectValue reflect.Value |
| 63 |
private bool |
| 64 |
created bool |
| 65 |
embedded bool |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func (o *Object) String() string { |
| 70 |
var buf bytes.Buffer |
| 71 |
fmt.Fprint(&buf, o.reflectType) |
| 72 |
if o.Name != "" { |
| 73 |
fmt.Fprintf(&buf, " named %s", o.Name) |
| 74 |
} |
| 75 |
return buf.String() |
| 76 |
} |
| 77 |
|
| 78 |
func (o *Object) addDep(field string, dep *Object) { |
| 79 |
if o.Fields == nil { |
| 80 |
o.Fields = make(map[string]*Object) |
| 81 |
} |
| 82 |
o.Fields[field] = dep |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
type Graph struct { |
| 87 |
Logger Logger |
| 88 |
unnamed []*Object |
| 89 |
unnamedType map[reflect.Type]bool |
| 90 |
named map[string]*Object |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
func (g *Graph) Provide(objects ...*Object) error { |
| 96 |
for _, o := range objects { |
| 97 |
o.reflectType = reflect.TypeOf(o.Value) |
| 98 |
o.reflectValue = reflect.ValueOf(o.Value) |
| 99 |
|
| 100 |
if o.Fields != nil { |
| 101 |
return fmt.Errorf( |
| 102 |
"fields were specified on object %s when it was provided", |
| 103 |
o, |
| 104 |
) |
| 105 |
} |
| 106 |
|
| 107 |
if o.Name == "" { |
| 108 |
if !isStructPtr(o.reflectType) { |
| 109 |
return fmt.Errorf( |
| 110 |
"expected unnamed object value to be a pointer to a struct but got type %s "+ |
| 111 |
"with value %v", |
| 112 |
o.reflectType, |
| 113 |
o.Value, |
| 114 |
) |
| 115 |
} |
| 116 |
|
| 117 |
if !o.private { |
| 118 |
if g.unnamedType == nil { |
| 119 |
g.unnamedType = make(map[reflect.Type]bool) |
| 120 |
} |
| 121 |
|
| 122 |
if g.unnamedType[o.reflectType] { |
| 123 |
return fmt.Errorf( |
| 124 |
"provided two unnamed instances of type *%s.%s", |
| 125 |
o.reflectType.Elem().PkgPath(), o.reflectType.Elem().Name(), |
| 126 |
) |
| 127 |
} |
| 128 |
g.unnamedType[o.reflectType] = true |
| 129 |
} |
| 130 |
g.unnamed = append(g.unnamed, o) |
| 131 |
} else { |
| 132 |
if g.named == nil { |
| 133 |
g.named = make(map[string]*Object) |
| 134 |
} |
| 135 |
|
| 136 |
if g.named[o.Name] != nil { |
| 137 |
return fmt.Errorf("provided two instances named %s", o.Name) |
| 138 |
} |
| 139 |
g.named[o.Name] = o |
| 140 |
} |
| 141 |
|
| 142 |
if g.Logger != nil { |
| 143 |
if o.created { |
| 144 |
g.Logger.Debugf("created %s", o) |
| 145 |
} else if o.embedded { |
| 146 |
g.Logger.Debugf("provided embedded %s", o) |
| 147 |
} else { |
| 148 |
g.Logger.Debugf("provided %s", o) |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
func (g *Graph) Populate() error { |
| 157 |
for _, o := range g.named { |
| 158 |
if o.Complete { |
| 159 |
continue |
| 160 |
} |
| 161 |
|
| 162 |
if err := g.populateExplicit(o); err != nil { |
| 163 |
return err |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
i := 0 |
| 170 |
for { |
| 171 |
if i == len(g.unnamed) { |
| 172 |
break |
| 173 |
} |
| 174 |
|
| 175 |
o := g.unnamed[i] |
| 176 |
i++ |
| 177 |
|
| 178 |
if o.Complete { |
| 179 |
continue |
| 180 |
} |
| 181 |
|
| 182 |
if err := g.populateExplicit(o); err != nil { |
| 183 |
return err |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
for _, o := range g.unnamed { |
| 190 |
if o.Complete { |
| 191 |
continue |
| 192 |
} |
| 193 |
|
| 194 |
if err := g.populateUnnamedInterface(o); err != nil { |
| 195 |
return err |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
for _, o := range g.named { |
| 200 |
if o.Complete { |
| 201 |
continue |
| 202 |
} |
| 203 |
|
| 204 |
if err := g.populateUnnamedInterface(o); err != nil { |
| 205 |
return err |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return nil |
| 210 |
} |
| 211 |
|
| 212 |
func (g *Graph) populateExplicit(o *Object) error { |
| 213 |
|
| 214 |
if o.Name != "" && !isStructPtr(o.reflectType) { |
| 215 |
return nil |
| 216 |
} |
| 217 |
|
| 218 |
StructLoop: |
| 219 |
for i := 0; i < o.reflectValue.Elem().NumField(); i++ { |
| 220 |
field := o.reflectValue.Elem().Field(i) |
| 221 |
fieldType := field.Type() |
| 222 |
fieldTag := o.reflectType.Elem().Field(i).Tag |
| 223 |
fieldName := o.reflectType.Elem().Field(i).Name |
| 224 |
tag, err := parseTag(string(fieldTag)) |
| 225 |
if err != nil { |
| 226 |
return fmt.Errorf( |
| 227 |
"unexpected tag format `%s` for field %s in type %s", |
| 228 |
string(fieldTag), |
| 229 |
o.reflectType.Elem().Field(i).Name, |
| 230 |
o.reflectType, |
| 231 |
) |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
if tag == nil { |
| 236 |
continue |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
if !field.CanSet() { |
| 241 |
return fmt.Errorf( |
| 242 |
"inject requested on unexported field %s in type %s", |
| 243 |
o.reflectType.Elem().Field(i).Name, |
| 244 |
o.reflectType, |
| 245 |
) |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
if tag.Inline && fieldType.Kind() != reflect.Struct { |
| 250 |
return fmt.Errorf( |
| 251 |
"inline requested on non inlined field %s in type %s", |
| 252 |
o.reflectType.Elem().Field(i).Name, |
| 253 |
o.reflectType, |
| 254 |
) |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
if !isNilOrZero(field, fieldType) { |
| 259 |
continue |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
if tag.Name != "" { |
| 264 |
existing := g.named[tag.Name] |
| 265 |
if existing == nil { |
| 266 |
return fmt.Errorf( |
| 267 |
"did not find object named %s required by field %s in type %s", |
| 268 |
tag.Name, |
| 269 |
o.reflectType.Elem().Field(i).Name, |
| 270 |
o.reflectType, |
| 271 |
) |
| 272 |
} |
| 273 |
|
| 274 |
if !existing.reflectType.AssignableTo(fieldType) { |
| 275 |
return fmt.Errorf( |
| 276 |
"object named %s of type %s is not assignable to field %s (%s) in type %s", |
| 277 |
tag.Name, |
| 278 |
fieldType, |
| 279 |
o.reflectType.Elem().Field(i).Name, |
| 280 |
existing.reflectType, |
| 281 |
o.reflectType, |
| 282 |
) |
| 283 |
} |
| 284 |
|
| 285 |
field.Set(reflect.ValueOf(existing.Value)) |
| 286 |
if g.Logger != nil { |
| 287 |
g.Logger.Debugf( |
| 288 |
"assigned %s to field %s in %s", |
| 289 |
existing, |
| 290 |
o.reflectType.Elem().Field(i).Name, |
| 291 |
o, |
| 292 |
) |
| 293 |
} |
| 294 |
o.addDep(fieldName, existing) |
| 295 |
continue StructLoop |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
if fieldType.Kind() == reflect.Struct { |
| 301 |
if tag.Private { |
| 302 |
return fmt.Errorf( |
| 303 |
"cannot use private inject on inline struct on field %s in type %s", |
| 304 |
o.reflectType.Elem().Field(i).Name, |
| 305 |
o.reflectType, |
| 306 |
) |
| 307 |
} |
| 308 |
|
| 309 |
if !tag.Inline { |
| 310 |
return fmt.Errorf( |
| 311 |
"inline struct on field %s in type %s requires an explicit \"inline\" tag", |
| 312 |
o.reflectType.Elem().Field(i).Name, |
| 313 |
o.reflectType, |
| 314 |
) |
| 315 |
} |
| 316 |
|
| 317 |
err := g.Provide(&Object{ |
| 318 |
Value: field.Addr().Interface(), |
| 319 |
private: true, |
| 320 |
embedded: o.reflectType.Elem().Field(i).Anonymous, |
| 321 |
}) |
| 322 |
if err != nil { |
| 323 |
return err |
| 324 |
} |
| 325 |
continue |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
if fieldType.Kind() == reflect.Interface { |
| 330 |
continue |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
if fieldType.Kind() == reflect.Map { |
| 335 |
if !tag.Private { |
| 336 |
return fmt.Errorf( |
| 337 |
"inject on map field %s in type %s must be named or private", |
| 338 |
o.reflectType.Elem().Field(i).Name, |
| 339 |
o.reflectType, |
| 340 |
) |
| 341 |
} |
| 342 |
|
| 343 |
field.Set(reflect.MakeMap(fieldType)) |
| 344 |
if g.Logger != nil { |
| 345 |
g.Logger.Debugf( |
| 346 |
"made map for field %s in %s", |
| 347 |
o.reflectType.Elem().Field(i).Name, |
| 348 |
o, |
| 349 |
) |
| 350 |
} |
| 351 |
continue |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
if !isStructPtr(fieldType) { |
| 356 |
return fmt.Errorf( |
| 357 |
"found inject tag on unsupported field %s in type %s", |
| 358 |
o.reflectType.Elem().Field(i).Name, |
| 359 |
o.reflectType, |
| 360 |
) |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
if !tag.Private { |
| 366 |
for _, existing := range g.unnamed { |
| 367 |
if existing.private { |
| 368 |
continue |
| 369 |
} |
| 370 |
if existing.reflectType.AssignableTo(fieldType) { |
| 371 |
field.Set(reflect.ValueOf(existing.Value)) |
| 372 |
if g.Logger != nil { |
| 373 |
g.Logger.Debugf( |
| 374 |
"assigned existing %s to field %s in %s", |
| 375 |
existing, |
| 376 |
o.reflectType.Elem().Field(i).Name, |
| 377 |
o, |
| 378 |
) |
| 379 |
} |
| 380 |
o.addDep(fieldName, existing) |
| 381 |
continue StructLoop |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
newValue := reflect.New(fieldType.Elem()) |
| 387 |
newObject := &Object{ |
| 388 |
Value: newValue.Interface(), |
| 389 |
private: tag.Private, |
| 390 |
created: true, |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
err = g.Provide(newObject) |
| 395 |
if err != nil { |
| 396 |
return err |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
field.Set(newValue) |
| 401 |
if g.Logger != nil { |
| 402 |
g.Logger.Debugf( |
| 403 |
"assigned newly created %s to field %s in %s", |
| 404 |
newObject, |
| 405 |
o.reflectType.Elem().Field(i).Name, |
| 406 |
o, |
| 407 |
) |
| 408 |
} |
| 409 |
o.addDep(fieldName, newObject) |
| 410 |
} |
| 411 |
return nil |
| 412 |
} |
| 413 |
|
| 414 |
func (g *Graph) populateUnnamedInterface(o *Object) error { |
| 415 |
|
| 416 |
if o.Name != "" && !isStructPtr(o.reflectType) { |
| 417 |
return nil |
| 418 |
} |
| 419 |
|
| 420 |
for i := 0; i < o.reflectValue.Elem().NumField(); i++ { |
| 421 |
field := o.reflectValue.Elem().Field(i) |
| 422 |
fieldType := field.Type() |
| 423 |
fieldTag := o.reflectType.Elem().Field(i).Tag |
| 424 |
fieldName := o.reflectType.Elem().Field(i).Name |
| 425 |
tag, err := parseTag(string(fieldTag)) |
| 426 |
if err != nil { |
| 427 |
return fmt.Errorf( |
| 428 |
"unexpected tag format `%s` for field %s in type %s", |
| 429 |
string(fieldTag), |
| 430 |
o.reflectType.Elem().Field(i).Name, |
| 431 |
o.reflectType, |
| 432 |
) |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
if tag == nil { |
| 437 |
continue |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
if fieldType.Kind() != reflect.Interface { |
| 443 |
continue |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
if tag.Private { |
| 449 |
return fmt.Errorf( |
| 450 |
"found private inject tag on interface field %s in type %s", |
| 451 |
o.reflectType.Elem().Field(i).Name, |
| 452 |
o.reflectType, |
| 453 |
) |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
if !isNilOrZero(field, fieldType) { |
| 458 |
continue |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
if tag.Name != "" { |
| 463 |
panic(fmt.Sprintf("unhandled named instance with name %s", tag.Name)) |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
var found *Object |
| 468 |
for _, existing := range g.unnamed { |
| 469 |
if existing.private { |
| 470 |
continue |
| 471 |
} |
| 472 |
if existing.reflectType.AssignableTo(fieldType) { |
| 473 |
if found != nil { |
| 474 |
return fmt.Errorf( |
| 475 |
"found two assignable values for field %s in type %s. one type "+ |
| 476 |
"%s with value %v and another type %s with value %v", |
| 477 |
o.reflectType.Elem().Field(i).Name, |
| 478 |
o.reflectType, |
| 479 |
found.reflectType, |
| 480 |
found.Value, |
| 481 |
existing.reflectType, |
| 482 |
existing.reflectValue, |
| 483 |
) |
| 484 |
} |
| 485 |
found = existing |
| 486 |
field.Set(reflect.ValueOf(existing.Value)) |
| 487 |
if g.Logger != nil { |
| 488 |
g.Logger.Debugf( |
| 489 |
"assigned existing %s to interface field %s in %s", |
| 490 |
existing, |
| 491 |
o.reflectType.Elem().Field(i).Name, |
| 492 |
o, |
| 493 |
) |
| 494 |
} |
| 495 |
o.addDep(fieldName, existing) |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
if found == nil { |
| 501 |
return fmt.Errorf( |
| 502 |
"found no assignable value for field %s in type %s", |
| 503 |
o.reflectType.Elem().Field(i).Name, |
| 504 |
o.reflectType, |
| 505 |
) |
| 506 |
} |
| 507 |
} |
| 508 |
return nil |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
func (g *Graph) Objects() []*Object { |
| 514 |
objects := make([]*Object, 0, len(g.unnamed)+len(g.named)) |
| 515 |
for _, o := range g.unnamed { |
| 516 |
if !o.embedded { |
| 517 |
objects = append(objects, o) |
| 518 |
} |
| 519 |
} |
| 520 |
for _, o := range g.named { |
| 521 |
if !o.embedded { |
| 522 |
objects = append(objects, o) |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
for i := 0; i < len(objects); i++ { |
| 527 |
j := rand.Intn(i + 1) |
| 528 |
objects[i], objects[j] = objects[j], objects[i] |
| 529 |
} |
| 530 |
return objects |
| 531 |
} |
| 532 |
|
| 533 |
var ( |
| 534 |
injectOnly = &tag{} |
| 535 |
injectPrivate = &tag{Private: true} |
| 536 |
injectInline = &tag{Inline: true} |
| 537 |
) |
| 538 |
|
| 539 |
type tag struct { |
| 540 |
Name string |
| 541 |
Inline bool |
| 542 |
Private bool |
| 543 |
} |
| 544 |
|
| 545 |
func parseTag(t string) (*tag, error) { |
| 546 |
found, value, err := structtag.Extract("inject", t) |
| 547 |
if err != nil { |
| 548 |
return nil, err |
| 549 |
} |
| 550 |
if !found { |
| 551 |
return nil, nil |
| 552 |
} |
| 553 |
if value == "" { |
| 554 |
return injectOnly, nil |
| 555 |
} |
| 556 |
if value == "inline" { |
| 557 |
return injectInline, nil |
| 558 |
} |
| 559 |
if value == "private" { |
| 560 |
return injectPrivate, nil |
| 561 |
} |
| 562 |
return &tag{Name: value}, nil |
| 563 |
} |
| 564 |
|
| 565 |
func isStructPtr(t reflect.Type) bool { |
| 566 |
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct |
| 567 |
} |
| 568 |
|
| 569 |
func isNilOrZero(v reflect.Value, t reflect.Type) bool { |
| 570 |
switch v.Kind() { |
| 571 |
default: |
| 572 |
return reflect.DeepEqual(v.Interface(), reflect.Zero(t).Interface()) |
| 573 |
case reflect.Interface, reflect.Ptr: |
| 574 |
return v.IsNil() |
| 575 |
} |
| 576 |
} |