migrate from github
This commit is contained in:
@@ -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