options.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. Schema bool
  15. Context context.Context
  16. LookupFunc func(ctx context.Context, ns string, moduleName string, tableName string, scene string) []*Schema
  17. }
  18. type Option func(o *Options)
  19. func WithContext(ctx context.Context) Option {
  20. return func(o *Options) {
  21. o.Context = ctx
  22. }
  23. }
  24. func WithoutSchema(ctx context.Context) Option {
  25. return func(o *Options) {
  26. o.Schema = false
  27. }
  28. }
  29. func WithDB(db *gorm.DB) Option {
  30. return func(o *Options) {
  31. o.DB = db
  32. }
  33. }
  34. func WithNamespace(ns string) Option {
  35. return func(o *Options) {
  36. o.EnableNamespace = true
  37. o.Namespace = ns
  38. }
  39. }
  40. func WithApiPrefix(prefix string) Option {
  41. return func(o *Options) {
  42. o.ApiPrefix = prefix
  43. }
  44. }
  45. func WithTablePrefix(prefix string) Option {
  46. return func(o *Options) {
  47. o.TablePrefix = prefix
  48. }
  49. }
  50. func newOptions() *Options {
  51. return &Options{
  52. Schema: true,
  53. Namespace: DefaultNamespace,
  54. Formatter: DefaultFormatter,
  55. }
  56. }