Organize backend into product executable and shared Go modules

This commit is contained in:
Stephan D committed 2026-09-12 14:14:08 +02:00
1 parent 981e84a4b4
commit f0720ec66b
382 files changed
+1944 -6989

No files matched your search

@@ -0,0 +1,45 @@
package architecture
import (
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"strconv"
"strings"
"testing"
)
// Provider access is HTTP only. Local Mongo repositories belong to SXCHNGE.
func TestNoSendicoRuntimeOrPrivateTransportImports(t *testing.T) {
err := filepath.WalkDir("../..", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || filepath.Ext(path) != ".go" {
return nil
}
f, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly)
if err != nil {
return err
}
for _, im := range f.Imports {
name, err := strconv.Unquote(im.Path.Value)
if err != nil {
return err
}
if strings.HasPrefix(filepath.ToSlash(path), "../../shared/") && strings.HasPrefix(name, "git.sendico.io/tech/sxchnge/") && !strings.HasPrefix(name, "git.sendico.io/tech/sxchnge/shared/") {
t.Errorf("shared library imports an executable module: %s in %s", name, path)
}
for _, blocked := range []string{"github.com/tech/sendico/", "google.golang.org/grpc", "github.com/nats-io/", "/pkg/discovery", "/pkg/proto/", "github.com/casbin/", "/pkg/auth", "/storage/organization", "/mongo/policiesdb", "/mongo/rolesdb", "/mongo/invitationdb"} {
if strings.Contains(name, blocked) {
t.Errorf("%s imports private provider transport or shared runtime: %s", path, name)
}
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
}
@@ -0,0 +1,47 @@
package architecture
import (
"go/ast"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"strings"
"testing"
)
func TestLocalCustomerModelHasNoPlatformTenancy(t *testing.T) {
// Local customer models and storage must stay free of provider tenancy.
forbidden := map[string]bool{"organization": true, "organizationref": true, "orgref": true, "orgdb": true, "role": true, "roles": true, "roledescription": true, "rolemanager": true, "policy": true, "policies": true, "policydescription": true, "policydb": true, "permissionbound": true, "signup_provisioning": true}
err := filepath.WalkDir("../..", func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() || filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {
return nil
}
parsed, err := parser.ParseFile(token.NewFileSet(), path, nil, 0)
if err != nil {
return err
}
ast.Inspect(parsed, func(node ast.Node) bool {
var names []*ast.Ident
switch n := node.(type) {
case *ast.TypeSpec:
names = []*ast.Ident{n.Name}
case *ast.Field:
names = n.Names
}
for _, name := range names {
if forbidden[strings.ToLower(name.Name)] {
t.Errorf("%s reintroduces local platform tenancy: %s", path, name.Name)
}
}
return true
})
return nil
})
if err != nil {
t.Fatal(err)
}
}
@@ -0,0 +1,69 @@
package architecture
import (
"go/ast"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"strconv"
"testing"
)
// Keep the backend error-model guideline executable, including aliased imports.
func TestErrorConstructorsUseProjectModel(t *testing.T) {
root := filepath.Join("..", "..")
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || filepath.Ext(path) != ".go" {
return nil
}
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return err
}
imports := map[string]string{}
for _, imp := range file.Imports {
pkg, err := strconv.Unquote(imp.Path.Value)
if err != nil {
return err
}
if pkg != "errors" && pkg != "fmt" {
continue
}
alias := pkg
if imp.Name != nil {
alias = imp.Name.Name
}
if alias == "." {
t.Errorf("%s: dot import obscures error constructors", path)
}
imports[alias] = pkg
}
ast.Inspect(file, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok {
return true
}
sel, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
ident, ok := sel.X.(*ast.Ident)
if !ok {
return true
}
if (imports[ident.Name] == "errors" && sel.Sel.Name == "New") || (imports[ident.Name] == "fmt" && sel.Sel.Name == "Errorf") {
t.Errorf("%s: use backend/shared/errors", fset.Position(call.Pos()))
}
return true
})
return nil
})
if err != nil {
t.Fatal(err)
}
}