1
0
mirror of https://github.com/go-micro/go-micro.git synced 2026-04-30 19:15:24 +02:00
Files
Asim Aslam 1bb25d6e7f Add agent platform showcase and refactor project structure (#2884)
* feat: add agent platform showcase and blog post

Add a complete platform example (Users, Posts, Comments, Mail) that
mirrors micro/blog, demonstrating how existing microservices become
AI-accessible through MCP with zero code changes.

Includes blog post "Your Microservices Are Already an AI Platform"
walking through real agent workflows: signup, content creation,
commenting, tagging, and cross-service messaging.

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

* refactor: rename handler types to drop redundant Service suffix

UserService → Users, PostService → Posts, CommentService → Comments,
MailService → Mail. Matches micro/blog naming convention.

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

* refactor: consolidate top-level directories, reduce framework bloat

Move internal/non-public packages behind internal/ or into their
parent packages where they belong:

- deploy/ → gateway/mcp/deploy/ (Helm charts belong with the gateway)
- profile/ → service/profile/ (preset plugin profiles are a service concern)
- scripts/ → internal/scripts/ (install script is not public API)
- test/ → internal/test/ (test harness is not public API)
- util/ → internal/util/ (internal helpers shouldn't be imported externally)

Also fixes CLAUDE.md merge conflict markers and updates project
structure documentation.

All import paths updated. Build and tests pass.

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

* refactor: redesign model package to match framework conventions

Rename model.Database interface to model.Model (consistent with
client.Client, server.Server, store.Store). Remove generics in
favor of interface{}-based API with reflection.

Key changes:
- model.Model interface: Register once, CRUD infers table from type
- DefaultModel + NewModel() + package-level convenience functions
- Schema registered via Register(&User{}), no per-call schema passing
- Memory implementation as default (in model package, like store)
- memory/sqlite/postgres backends updated for new interface
- protoc-gen-micro generates RegisterXModel() instead of generic factory
- All docs, blog, and README updated

https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-05 11:21:41 +00:00

139 lines
3.1 KiB
Go

package model
import (
"testing"
)
type TestUser struct {
ID string `json:"id" model:"key"`
Name string `json:"name" model:"index"`
Email string `json:"email"`
Age int `json:"age"`
}
func TestBuildSchema(t *testing.T) {
schema := BuildSchema(TestUser{})
if schema.Table != "testusers" {
t.Errorf("expected table 'testusers', got %q", schema.Table)
}
if schema.Key != "id" {
t.Errorf("expected key 'id', got %q", schema.Key)
}
if len(schema.Fields) != 4 {
t.Fatalf("expected 4 fields, got %d", len(schema.Fields))
}
var keyField Field
var indexField Field
for _, f := range schema.Fields {
if f.IsKey {
keyField = f
}
if f.Index {
indexField = f
}
}
if keyField.Column != "id" {
t.Errorf("expected key column 'id', got %q", keyField.Column)
}
if indexField.Column != "name" {
t.Errorf("expected index column 'name', got %q", indexField.Column)
}
}
func TestBuildSchema_DefaultKey(t *testing.T) {
type Item struct {
ID string `json:"id"`
Name string `json:"name"`
}
schema := BuildSchema(Item{})
if schema.Key != "id" {
t.Errorf("expected default key 'id', got %q", schema.Key)
}
}
func TestBuildSchema_WithTable(t *testing.T) {
schema := BuildSchema(TestUser{}, WithTable("my_users"))
if schema.Table != "my_users" {
t.Errorf("expected table 'my_users', got %q", schema.Table)
}
}
func TestStructToMap(t *testing.T) {
schema := BuildSchema(TestUser{})
u := &TestUser{ID: "1", Name: "Alice", Email: "alice@example.com", Age: 30}
m := StructToMap(schema, u)
if m["id"] != "1" {
t.Errorf("expected id '1', got %v", m["id"])
}
if m["name"] != "Alice" {
t.Errorf("expected name 'Alice', got %v", m["name"])
}
if m["email"] != "alice@example.com" {
t.Errorf("expected email 'alice@example.com', got %v", m["email"])
}
if m["age"] != 30 {
t.Errorf("expected age 30, got %v", m["age"])
}
}
func TestMapToStruct(t *testing.T) {
schema := BuildSchema(TestUser{})
m := map[string]any{
"id": "1",
"name": "Bob",
"email": "bob@example.com",
"age": 25,
}
u := &TestUser{}
MapToStruct(schema, m, u)
if u.ID != "1" {
t.Errorf("expected ID '1', got %q", u.ID)
}
if u.Name != "Bob" {
t.Errorf("expected Name 'Bob', got %q", u.Name)
}
if u.Email != "bob@example.com" {
t.Errorf("expected Email 'bob@example.com', got %q", u.Email)
}
if u.Age != 25 {
t.Errorf("expected Age 25, got %d", u.Age)
}
}
func TestApplyQueryOptions(t *testing.T) {
q := ApplyQueryOptions(
Where("name", "Alice"),
WhereOp("age", ">", 20),
OrderDesc("name"),
Limit(10),
Offset(5),
)
if len(q.Filters) != 2 {
t.Fatalf("expected 2 filters, got %d", len(q.Filters))
}
if q.Filters[0].Field != "name" || q.Filters[0].Op != "=" || q.Filters[0].Value != "Alice" {
t.Errorf("unexpected filter 0: %+v", q.Filters[0])
}
if q.Filters[1].Field != "age" || q.Filters[1].Op != ">" {
t.Errorf("unexpected filter 1: %+v", q.Filters[1])
}
if q.OrderBy != "name" || !q.Desc {
t.Errorf("expected order by name desc, got %q desc=%v", q.OrderBy, q.Desc)
}
if q.Limit != 10 {
t.Errorf("expected limit 10, got %d", q.Limit)
}
if q.Offset != 5 {
t.Errorf("expected offset 5, got %d", q.Offset)
}
}