No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

register.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package phi
  2. import "sync"
  3. var (
  4. driversMutex sync.RWMutex
  5. drivers = make(map[string]Renderer)
  6. )
  7. // Register a new RenderFunc for the given content types.
  8. func Register(driver Renderer, contentTypes ...string) {
  9. driversMutex.Lock()
  10. defer driversMutex.Unlock()
  11. if driver == nil {
  12. panic(`content: driver is nil`)
  13. }
  14. if len(contentTypes) == 0 {
  15. panic(`content: no content types`)
  16. }
  17. for _, contentType := range contentTypes {
  18. if _, dupe := drivers[contentType]; dupe {
  19. panic(`content: Register called twice for content type ` + contentType)
  20. }
  21. drivers[contentType] = driver
  22. }
  23. }
  24. // Unregister removes a Renderer from the drivers.
  25. func Unregister(driver Renderer) {
  26. driversMutex.Lock()
  27. defer driversMutex.Unlock()
  28. if driver == nil {
  29. panic(`content: driver is nil`)
  30. }
  31. for contentType, other := range drivers {
  32. if other == driver {
  33. delete(drivers, contentType)
  34. }
  35. }
  36. }
  37. // UnregisterContentType removes the RenderFunc for the given content type. If
  38. // the content type is not defined, we fail silently.
  39. func UnregisterContentType(contentType string) {
  40. driversMutex.Lock()
  41. defer driversMutex.Unlock()
  42. if _, ok := drivers[contentType]; ok {
  43. delete(drivers, contentType)
  44. }
  45. }