migrate from github
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package doc_container_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type knowledgeBaseAdapter struct{}
|
||||
|
||||
func (knowledgeBaseAdapter) Name() string {
|
||||
return string(dto.ContainerType_KnowledgeBase)
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) ProtoType() pb.CreateDocumentContainerType {
|
||||
return pb.CreateDocumentContainerType_CREATE_DOCUMENT_CONTAINER_TYPE_KNOWLEDGE_BASE
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) DTOType() dto.ContainerType {
|
||||
return dto.ContainerType_KnowledgeBase
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) ModelType() model.ContainerType {
|
||||
return model.ContainerType_KnowledgeBase
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) RequiresKnowledgeBaseID() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(knowledgeBaseAdapter{}) }
|
||||
@@ -0,0 +1,31 @@
|
||||
package doc_container_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type ownAdapter struct{}
|
||||
|
||||
func (ownAdapter) Name() string {
|
||||
return string(dto.ContainerType_Own)
|
||||
}
|
||||
|
||||
func (ownAdapter) ProtoType() pb.CreateDocumentContainerType {
|
||||
return pb.CreateDocumentContainerType_CREATE_DOCUMENT_CONTAINER_TYPE_OWN
|
||||
}
|
||||
|
||||
func (ownAdapter) DTOType() dto.ContainerType {
|
||||
return dto.ContainerType_Own
|
||||
}
|
||||
|
||||
func (ownAdapter) ModelType() model.ContainerType {
|
||||
return model.ContainerType_Own
|
||||
}
|
||||
|
||||
func (ownAdapter) RequiresKnowledgeBaseID() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(ownAdapter{}) }
|
||||
@@ -0,0 +1,168 @@
|
||||
package doc_container_mapper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
const defaultContainerName = "own"
|
||||
|
||||
// Mapper defines a document container type mapping across transport(dto/proto) and model layers.
|
||||
type Mapper interface {
|
||||
Name() string
|
||||
ProtoType() pb.CreateDocumentContainerType
|
||||
DTOType() dto.ContainerType
|
||||
ModelType() model.ContainerType
|
||||
RequiresKnowledgeBaseID() bool
|
||||
}
|
||||
|
||||
type registry struct {
|
||||
byName map[string]Mapper
|
||||
byProto map[pb.CreateDocumentContainerType]Mapper
|
||||
byDTO map[dto.ContainerType]Mapper
|
||||
byModel map[model.ContainerType]Mapper
|
||||
}
|
||||
|
||||
var globalRegistry = ®istry{
|
||||
byName: make(map[string]Mapper),
|
||||
byProto: make(map[pb.CreateDocumentContainerType]Mapper),
|
||||
byDTO: make(map[dto.ContainerType]Mapper),
|
||||
byModel: make(map[model.ContainerType]Mapper),
|
||||
}
|
||||
|
||||
func RegisterMapper(mapper Mapper) {
|
||||
if err := globalRegistry.register(mapper); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *registry) register(mapper Mapper) error {
|
||||
if mapper == nil {
|
||||
return fmt.Errorf("document container mapper is nil")
|
||||
}
|
||||
name := normalizeName(mapper.Name())
|
||||
if name == "" {
|
||||
return fmt.Errorf("document container mapper name is empty")
|
||||
}
|
||||
|
||||
protoType := mapper.ProtoType()
|
||||
dtoType := mapper.DTOType()
|
||||
modelType := mapper.ModelType()
|
||||
if protoType == pb.CreateDocumentContainerType_CREATE_DOCUMENT_CONTAINER_TYPE_UNSPECIFIED || dtoType == "" || modelType == "" {
|
||||
return fmt.Errorf("document container mapper %q has invalid mapping", name)
|
||||
}
|
||||
|
||||
if _, exists := r.byName[name]; exists {
|
||||
return fmt.Errorf("document container mapper %q already registered", name)
|
||||
}
|
||||
if _, exists := r.byProto[protoType]; exists {
|
||||
return fmt.Errorf("document container proto mapping %v already registered", protoType)
|
||||
}
|
||||
if _, exists := r.byDTO[dtoType]; exists {
|
||||
return fmt.Errorf("document container dto mapping %q already registered", dtoType)
|
||||
}
|
||||
if _, exists := r.byModel[modelType]; exists {
|
||||
return fmt.Errorf("document container model mapping %q already registered", modelType)
|
||||
}
|
||||
|
||||
r.byName[name] = mapper
|
||||
r.byProto[protoType] = mapper
|
||||
r.byDTO[dtoType] = mapper
|
||||
r.byModel[modelType] = mapper
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeName(name string) string {
|
||||
return strings.ToLower(strings.TrimSpace(name))
|
||||
}
|
||||
|
||||
func (r *registry) findByName(name string) Mapper {
|
||||
return r.byName[normalizeName(name)]
|
||||
}
|
||||
|
||||
func (r *registry) findByProto(t pb.CreateDocumentContainerType) Mapper {
|
||||
return r.byProto[t]
|
||||
}
|
||||
|
||||
func (r *registry) findByDTO(t dto.ContainerType) Mapper {
|
||||
if mapper, ok := r.byDTO[t]; ok {
|
||||
return mapper
|
||||
}
|
||||
return r.byName[normalizeName(string(t))]
|
||||
}
|
||||
|
||||
func (r *registry) findByModel(t model.ContainerType) Mapper {
|
||||
if mapper, ok := r.byModel[t]; ok {
|
||||
return mapper
|
||||
}
|
||||
return r.byName[normalizeName(string(t))]
|
||||
}
|
||||
|
||||
func (r *registry) defaultMapper() Mapper {
|
||||
if mapper := r.findByName(defaultContainerName); mapper != nil {
|
||||
return mapper
|
||||
}
|
||||
for _, mapper := range r.byName {
|
||||
return mapper
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DefaultDTOType() dto.ContainerType {
|
||||
mapper := globalRegistry.defaultMapper()
|
||||
if mapper == nil {
|
||||
return dto.ContainerType(defaultContainerName)
|
||||
}
|
||||
return mapper.DTOType()
|
||||
}
|
||||
|
||||
func IsSupportedDTOType(t dto.ContainerType) bool {
|
||||
return globalRegistry.findByDTO(t) != nil
|
||||
}
|
||||
|
||||
func IsSupportedProtoType(t pb.CreateDocumentContainerType) bool {
|
||||
return globalRegistry.findByProto(t) != nil
|
||||
}
|
||||
|
||||
func FromProtoType(t pb.CreateDocumentContainerType) (dto.ContainerType, bool) {
|
||||
if mapper := globalRegistry.findByProto(t); mapper != nil {
|
||||
return mapper.DTOType(), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func ToProtoType(t dto.ContainerType) pb.CreateDocumentContainerType {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.ProtoType()
|
||||
}
|
||||
return pb.CreateDocumentContainerType_CREATE_DOCUMENT_CONTAINER_TYPE_UNSPECIFIED
|
||||
}
|
||||
|
||||
func ToModelType(t dto.ContainerType) model.ContainerType {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.ModelType()
|
||||
}
|
||||
return model.ContainerType(DefaultDTOType())
|
||||
}
|
||||
|
||||
func FromModelType(t model.ContainerType) dto.ContainerType {
|
||||
if mapper := globalRegistry.findByModel(t); mapper != nil {
|
||||
return mapper.DTOType()
|
||||
}
|
||||
normalized := normalizeName(string(t))
|
||||
if normalized != "" {
|
||||
return dto.ContainerType(normalized)
|
||||
}
|
||||
return DefaultDTOType()
|
||||
}
|
||||
|
||||
func RequiresKnowledgeBaseID(t dto.ContainerType) bool {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.RequiresKnowledgeBaseID()
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package doc_type_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type markdownAdapter struct{}
|
||||
|
||||
func (markdownAdapter) Name() string {
|
||||
return string(dto.DocumentType_Markdown)
|
||||
}
|
||||
|
||||
func (markdownAdapter) ProtoType() pb.DocumentType {
|
||||
return pb.DocumentType_DOCUMENT_TYPE_MARKDOWN
|
||||
}
|
||||
|
||||
func (markdownAdapter) DTOType() dto.DocumentType {
|
||||
return dto.DocumentType_Markdown
|
||||
}
|
||||
|
||||
func (markdownAdapter) ModelType() model.DocumentType {
|
||||
return model.DocumentType_Markdown
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(markdownAdapter{}) }
|
||||
@@ -0,0 +1,27 @@
|
||||
package doc_type_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type markdownSlideAdapter struct{}
|
||||
|
||||
func (markdownSlideAdapter) Name() string {
|
||||
return string(dto.DocumentType_MarkdownSlide)
|
||||
}
|
||||
|
||||
func (markdownSlideAdapter) ProtoType() pb.DocumentType {
|
||||
return pb.DocumentType_DOCUMENT_TYPE_MARKDOWN_SLIDE
|
||||
}
|
||||
|
||||
func (markdownSlideAdapter) DTOType() dto.DocumentType {
|
||||
return dto.DocumentType_MarkdownSlide
|
||||
}
|
||||
|
||||
func (markdownSlideAdapter) ModelType() model.DocumentType {
|
||||
return model.DocumentType_MarkdownSlide
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(markdownSlideAdapter{}) }
|
||||
@@ -0,0 +1,167 @@
|
||||
package doc_type_mapper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
const defaultTypeName = "markdown"
|
||||
|
||||
// Mapper defines a document type mapping across transport(dto/proto) and model layers.
|
||||
type Mapper interface {
|
||||
Name() string
|
||||
ProtoType() pb.DocumentType
|
||||
DTOType() dto.DocumentType
|
||||
ModelType() model.DocumentType
|
||||
}
|
||||
|
||||
type registry struct {
|
||||
byName map[string]Mapper
|
||||
byProto map[pb.DocumentType]Mapper
|
||||
byDTO map[dto.DocumentType]Mapper
|
||||
byModel map[model.DocumentType]Mapper
|
||||
}
|
||||
|
||||
var globalRegistry = ®istry{
|
||||
byName: make(map[string]Mapper),
|
||||
byProto: make(map[pb.DocumentType]Mapper),
|
||||
byDTO: make(map[dto.DocumentType]Mapper),
|
||||
byModel: make(map[model.DocumentType]Mapper),
|
||||
}
|
||||
|
||||
// RegisterMapper registers a document type mapper.
|
||||
// Implementing a new type only requires adding a new mapper and calling this in init().
|
||||
func RegisterMapper(mapper Mapper) {
|
||||
if err := globalRegistry.register(mapper); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *registry) register(mapper Mapper) error {
|
||||
if mapper == nil {
|
||||
return fmt.Errorf("document type mapper is nil")
|
||||
}
|
||||
|
||||
name := normalizeName(mapper.Name())
|
||||
if name == "" {
|
||||
return fmt.Errorf("document type mapper name is empty")
|
||||
}
|
||||
|
||||
dtoType := mapper.DTOType()
|
||||
modelType := mapper.ModelType()
|
||||
protoType := mapper.ProtoType()
|
||||
if dtoType == "" || modelType == "" || protoType == pb.DocumentType_DOCUMENT_TYPE_UNSPECIFIED {
|
||||
return fmt.Errorf("document type mapper %q has invalid mapping", name)
|
||||
}
|
||||
|
||||
if _, exists := r.byName[name]; exists {
|
||||
return fmt.Errorf("document type mapper %q already registered", name)
|
||||
}
|
||||
if _, exists := r.byProto[protoType]; exists {
|
||||
return fmt.Errorf("document type proto mapping %v already registered", protoType)
|
||||
}
|
||||
if _, exists := r.byDTO[dtoType]; exists {
|
||||
return fmt.Errorf("document type dto mapping %q already registered", dtoType)
|
||||
}
|
||||
if _, exists := r.byModel[modelType]; exists {
|
||||
return fmt.Errorf("document type model mapping %q already registered", modelType)
|
||||
}
|
||||
|
||||
r.byName[name] = mapper
|
||||
r.byProto[protoType] = mapper
|
||||
r.byDTO[dtoType] = mapper
|
||||
r.byModel[modelType] = mapper
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *registry) findByName(name string) Mapper {
|
||||
return r.byName[normalizeName(name)]
|
||||
}
|
||||
|
||||
func (r *registry) findByProto(t pb.DocumentType) Mapper {
|
||||
return r.byProto[t]
|
||||
}
|
||||
|
||||
func (r *registry) findByDTO(t dto.DocumentType) Mapper {
|
||||
if mapper, ok := r.byDTO[t]; ok {
|
||||
return mapper
|
||||
}
|
||||
return r.byName[normalizeName(string(t))]
|
||||
}
|
||||
|
||||
func (r *registry) findByModel(t model.DocumentType) Mapper {
|
||||
if mapper, ok := r.byModel[t]; ok {
|
||||
return mapper
|
||||
}
|
||||
return r.byName[normalizeName(string(t))]
|
||||
}
|
||||
|
||||
func (r *registry) defaultMapper() Mapper {
|
||||
if mapper := r.findByName(defaultTypeName); mapper != nil {
|
||||
return mapper
|
||||
}
|
||||
for _, mapper := range r.byName {
|
||||
return mapper
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeName(name string) string {
|
||||
normalized := strings.ToLower(strings.TrimSpace(name))
|
||||
switch normalized {
|
||||
case "slide":
|
||||
return "markdown_slide"
|
||||
case "yoreseerichtext", "yoresee-rich-text":
|
||||
return "yoresee_rich_text"
|
||||
default:
|
||||
return normalized
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultDTOType() dto.DocumentType {
|
||||
mapper := globalRegistry.defaultMapper()
|
||||
if mapper == nil {
|
||||
return dto.DocumentType(defaultTypeName)
|
||||
}
|
||||
return mapper.DTOType()
|
||||
}
|
||||
|
||||
func IsSupportedDTOType(t dto.DocumentType) bool {
|
||||
return globalRegistry.findByDTO(t) != nil
|
||||
}
|
||||
|
||||
func FromProtoType(t pb.DocumentType) dto.DocumentType {
|
||||
if mapper := globalRegistry.findByProto(t); mapper != nil {
|
||||
return mapper.DTOType()
|
||||
}
|
||||
return DefaultDTOType()
|
||||
}
|
||||
|
||||
func ToProtoType(t dto.DocumentType) pb.DocumentType {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.ProtoType()
|
||||
}
|
||||
return pb.DocumentType_DOCUMENT_TYPE_UNSPECIFIED
|
||||
}
|
||||
|
||||
func ToModelType(t dto.DocumentType) model.DocumentType {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.ModelType()
|
||||
}
|
||||
return model.DocumentType(DefaultDTOType())
|
||||
}
|
||||
|
||||
func FromModelType(t model.DocumentType) dto.DocumentType {
|
||||
if mapper := globalRegistry.findByModel(t); mapper != nil {
|
||||
return mapper.DTOType()
|
||||
}
|
||||
normalized := normalizeName(string(t))
|
||||
if normalized != "" {
|
||||
return dto.DocumentType(normalized)
|
||||
}
|
||||
return DefaultDTOType()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package doc_type_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type tableAdapter struct{}
|
||||
|
||||
func (tableAdapter) Name() string {
|
||||
return string(dto.DocumentType_Table)
|
||||
}
|
||||
|
||||
func (tableAdapter) ProtoType() pb.DocumentType {
|
||||
return pb.DocumentType_DOCUMENT_TYPE_TABLE
|
||||
}
|
||||
|
||||
func (tableAdapter) DTOType() dto.DocumentType {
|
||||
return dto.DocumentType_Table
|
||||
}
|
||||
|
||||
func (tableAdapter) ModelType() model.DocumentType {
|
||||
return model.DocumentType_Table
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(tableAdapter{}) }
|
||||
@@ -0,0 +1,27 @@
|
||||
package doc_type_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
"github.com/XingfenD/yoresee_doc/internal/model"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type yoreseeRichTextAdapter struct{}
|
||||
|
||||
func (yoreseeRichTextAdapter) Name() string {
|
||||
return string(dto.DocumentType_YoreseeRichText)
|
||||
}
|
||||
|
||||
func (yoreseeRichTextAdapter) ProtoType() pb.DocumentType {
|
||||
return pb.DocumentType_DOCUMENT_TYPE_YORESEE_RICH_TEXT
|
||||
}
|
||||
|
||||
func (yoreseeRichTextAdapter) DTOType() dto.DocumentType {
|
||||
return dto.DocumentType_YoreseeRichText
|
||||
}
|
||||
|
||||
func (yoreseeRichTextAdapter) ModelType() model.DocumentType {
|
||||
return model.DocumentType_YoreseeRichText
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(yoreseeRichTextAdapter{}) }
|
||||
@@ -0,0 +1,34 @@
|
||||
package template_container_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type knowledgeBaseAdapter struct{}
|
||||
|
||||
func (knowledgeBaseAdapter) Name() string {
|
||||
return "knowledge_base"
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) ProtoType() pb.CreateTemplateContainer {
|
||||
return pb.CreateTemplateContainer_KNOWLEDGEBASE_TEMPLATE
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) DTOType() dto.TemplateContainer {
|
||||
return dto.TemplateContainerKnowledgeBase
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) Scope() string {
|
||||
return "knowledge_base"
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) IsPublic() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (knowledgeBaseAdapter) RequiresKnowledgeBaseID() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(knowledgeBaseAdapter{}) }
|
||||
@@ -0,0 +1,34 @@
|
||||
package template_container_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type ownAdapter struct{}
|
||||
|
||||
func (ownAdapter) Name() string {
|
||||
return "own"
|
||||
}
|
||||
|
||||
func (ownAdapter) ProtoType() pb.CreateTemplateContainer {
|
||||
return pb.CreateTemplateContainer_OWN_TEMPLATE
|
||||
}
|
||||
|
||||
func (ownAdapter) DTOType() dto.TemplateContainer {
|
||||
return dto.TemplateContainerOwn
|
||||
}
|
||||
|
||||
func (ownAdapter) Scope() string {
|
||||
return "private"
|
||||
}
|
||||
|
||||
func (ownAdapter) IsPublic() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (ownAdapter) RequiresKnowledgeBaseID() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(ownAdapter{}) }
|
||||
@@ -0,0 +1,34 @@
|
||||
package template_container_mapper
|
||||
|
||||
import (
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
type publicAdapter struct{}
|
||||
|
||||
func (publicAdapter) Name() string {
|
||||
return "public"
|
||||
}
|
||||
|
||||
func (publicAdapter) ProtoType() pb.CreateTemplateContainer {
|
||||
return pb.CreateTemplateContainer_PUBLIC_TEMPLATE
|
||||
}
|
||||
|
||||
func (publicAdapter) DTOType() dto.TemplateContainer {
|
||||
return dto.TemplateContainerPublic
|
||||
}
|
||||
|
||||
func (publicAdapter) Scope() string {
|
||||
return "system"
|
||||
}
|
||||
|
||||
func (publicAdapter) IsPublic() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (publicAdapter) RequiresKnowledgeBaseID() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func init() { RegisterMapper(publicAdapter{}) }
|
||||
@@ -0,0 +1,151 @@
|
||||
package template_container_mapper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/XingfenD/yoresee_doc/internal/dto"
|
||||
pb "github.com/XingfenD/yoresee_doc/pkg/gen/yoresee_doc/v1"
|
||||
)
|
||||
|
||||
const defaultContainerName = "own"
|
||||
|
||||
type Mapper interface {
|
||||
Name() string
|
||||
ProtoType() pb.CreateTemplateContainer
|
||||
DTOType() dto.TemplateContainer
|
||||
Scope() string
|
||||
IsPublic() bool
|
||||
RequiresKnowledgeBaseID() bool
|
||||
}
|
||||
|
||||
type registry struct {
|
||||
byName map[string]Mapper
|
||||
byProto map[pb.CreateTemplateContainer]Mapper
|
||||
byDTO map[dto.TemplateContainer]Mapper
|
||||
}
|
||||
|
||||
var globalRegistry = ®istry{
|
||||
byName: make(map[string]Mapper),
|
||||
byProto: make(map[pb.CreateTemplateContainer]Mapper),
|
||||
byDTO: make(map[dto.TemplateContainer]Mapper),
|
||||
}
|
||||
|
||||
func RegisterMapper(mapper Mapper) {
|
||||
if err := globalRegistry.register(mapper); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *registry) register(mapper Mapper) error {
|
||||
if mapper == nil {
|
||||
return fmt.Errorf("template container mapper is nil")
|
||||
}
|
||||
name := normalizeName(mapper.Name())
|
||||
if name == "" {
|
||||
return fmt.Errorf("template container mapper name is empty")
|
||||
}
|
||||
|
||||
protoType := mapper.ProtoType()
|
||||
dtoType := mapper.DTOType()
|
||||
if dtoType == "" {
|
||||
return fmt.Errorf("template container mapper %q has invalid dto mapping", name)
|
||||
}
|
||||
if strings.TrimSpace(mapper.Scope()) == "" {
|
||||
return fmt.Errorf("template container mapper %q has empty scope mapping", name)
|
||||
}
|
||||
|
||||
if _, exists := r.byName[name]; exists {
|
||||
return fmt.Errorf("template container mapper %q already registered", name)
|
||||
}
|
||||
if _, exists := r.byProto[protoType]; exists {
|
||||
return fmt.Errorf("template container proto mapping %v already registered", protoType)
|
||||
}
|
||||
if _, exists := r.byDTO[dtoType]; exists {
|
||||
return fmt.Errorf("template container dto mapping %q already registered", dtoType)
|
||||
}
|
||||
|
||||
r.byName[name] = mapper
|
||||
r.byProto[protoType] = mapper
|
||||
r.byDTO[dtoType] = mapper
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeName(name string) string {
|
||||
return strings.ToLower(strings.TrimSpace(name))
|
||||
}
|
||||
|
||||
func (r *registry) findByName(name string) Mapper {
|
||||
return r.byName[normalizeName(name)]
|
||||
}
|
||||
|
||||
func (r *registry) findByProto(t pb.CreateTemplateContainer) Mapper {
|
||||
return r.byProto[t]
|
||||
}
|
||||
|
||||
func (r *registry) findByDTO(t dto.TemplateContainer) Mapper {
|
||||
return r.byDTO[t]
|
||||
}
|
||||
|
||||
func (r *registry) defaultMapper() Mapper {
|
||||
if mapper := r.findByName(defaultContainerName); mapper != nil {
|
||||
return mapper
|
||||
}
|
||||
for _, mapper := range r.byName {
|
||||
return mapper
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DefaultDTOType() dto.TemplateContainer {
|
||||
mapper := globalRegistry.defaultMapper()
|
||||
if mapper == nil {
|
||||
return dto.TemplateContainerOwn
|
||||
}
|
||||
return mapper.DTOType()
|
||||
}
|
||||
|
||||
func IsSupportedDTOType(t dto.TemplateContainer) bool {
|
||||
return globalRegistry.findByDTO(t) != nil
|
||||
}
|
||||
|
||||
func FromProtoType(t pb.CreateTemplateContainer) dto.TemplateContainer {
|
||||
if mapper := globalRegistry.findByProto(t); mapper != nil {
|
||||
return mapper.DTOType()
|
||||
}
|
||||
return DefaultDTOType()
|
||||
}
|
||||
|
||||
func ToProtoType(t dto.TemplateContainer) pb.CreateTemplateContainer {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.ProtoType()
|
||||
}
|
||||
return pb.CreateTemplateContainer_OWN_TEMPLATE
|
||||
}
|
||||
|
||||
func ToScope(t dto.TemplateContainer) string {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.Scope()
|
||||
}
|
||||
if mapper := globalRegistry.defaultMapper(); mapper != nil {
|
||||
return mapper.Scope()
|
||||
}
|
||||
return "private"
|
||||
}
|
||||
|
||||
func ToIsPublic(t dto.TemplateContainer) bool {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.IsPublic()
|
||||
}
|
||||
if mapper := globalRegistry.defaultMapper(); mapper != nil {
|
||||
return mapper.IsPublic()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func RequiresKnowledgeBaseID(t dto.TemplateContainer) bool {
|
||||
if mapper := globalRegistry.findByDTO(t); mapper != nil {
|
||||
return mapper.RequiresKnowledgeBaseID()
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user