options.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package rest
  2. import (
  3. "context"
  4. "gorm.io/gorm"
  5. )
  6. type Options struct {
  7. EnableNamespace bool
  8. Namespace string
  9. ApiPrefix string
  10. TablePrefix string
  11. DB *gorm.DB
  12. Formatter *Formatter
  13. Delegate *delegate
  14. Context context.Context
  15. LookupFunc func(ctx context.Context, ns string, moduleName string, tableName string, scene string) []*Schema
  16. }
  17. type Option func(o *Options)
  18. func WithContext(ctx context.Context) Option {
  19. return func(o *Options) {
  20. o.Context = ctx
  21. }
  22. }
  23. func WithDB(db *gorm.DB) Option {
  24. return func(o *Options) {
  25. o.DB = db
  26. }
  27. }
  28. func WithNamespace(ns string) Option {
  29. return func(o *Options) {
  30. o.EnableNamespace = true
  31. o.Namespace = ns
  32. }
  33. }
  34. func WithApiPrefix(prefix string) Option {
  35. return func(o *Options) {
  36. o.ApiPrefix = prefix
  37. }
  38. }
  39. func WithTablePrefix(prefix string) Option {
  40. return func(o *Options) {
  41. o.TablePrefix = prefix
  42. }
  43. }
  44. func newOptions() *Options {
  45. return &Options{
  46. Namespace: DefaultNamespace,
  47. Formatter: DefaultFormatter,
  48. }
  49. }