2026-03-25 18:39:36 +00:00
|
|
|
package core_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
. "dappco.re/go/core"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ExampleEntitlement_UsagePercent() {
|
|
|
|
|
e := Entitlement{Limit: 100, Used: 75}
|
feat: eliminate fmt, string concat — add core.Println, use Concat/Path everywhere
New primitive: core.Println() wraps fmt.Println.
Replaced across all test + example files:
- fmt.Println → Println (17 example files)
- fmt.Sprintf → Concat + Sprint
- dir + "/file" → Path(dir, "file") (path security)
- "str" + var → Concat("str", var) (AX consistency)
"fmt" import is now zero across all test files.
String concat with + is zero across all test files.
Remaining 9 stdlib imports (all Go infrastructure):
testing, context, time, sync, embed, io/fs, bytes, gzip, base64
558 tests, 84.5% coverage.
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-25 19:42:39 +00:00
|
|
|
Println(e.UsagePercent())
|
2026-03-25 18:39:36 +00:00
|
|
|
// Output: 75
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ExampleCore_SetEntitlementChecker() {
|
|
|
|
|
c := New()
|
|
|
|
|
c.SetEntitlementChecker(func(action string, qty int, _ context.Context) Entitlement {
|
|
|
|
|
limits := map[string]int{"social.accounts": 5, "ai.credits": 100}
|
|
|
|
|
usage := map[string]int{"social.accounts": 3, "ai.credits": 95}
|
|
|
|
|
|
|
|
|
|
limit, ok := limits[action]
|
|
|
|
|
if !ok {
|
|
|
|
|
return Entitlement{Allowed: false, Reason: "not in package"}
|
|
|
|
|
}
|
|
|
|
|
used := usage[action]
|
|
|
|
|
remaining := limit - used
|
|
|
|
|
if qty > remaining {
|
|
|
|
|
return Entitlement{Allowed: false, Limit: limit, Used: used, Remaining: remaining, Reason: "limit exceeded"}
|
|
|
|
|
}
|
|
|
|
|
return Entitlement{Allowed: true, Limit: limit, Used: used, Remaining: remaining}
|
|
|
|
|
})
|
|
|
|
|
|
feat: eliminate fmt, string concat — add core.Println, use Concat/Path everywhere
New primitive: core.Println() wraps fmt.Println.
Replaced across all test + example files:
- fmt.Println → Println (17 example files)
- fmt.Sprintf → Concat + Sprint
- dir + "/file" → Path(dir, "file") (path security)
- "str" + var → Concat("str", var) (AX consistency)
"fmt" import is now zero across all test files.
String concat with + is zero across all test files.
Remaining 9 stdlib imports (all Go infrastructure):
testing, context, time, sync, embed, io/fs, bytes, gzip, base64
558 tests, 84.5% coverage.
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-25 19:42:39 +00:00
|
|
|
Println(c.Entitled("social.accounts", 2).Allowed)
|
|
|
|
|
Println(c.Entitled("social.accounts", 5).Allowed)
|
|
|
|
|
Println(c.Entitled("ai.credits").NearLimit(0.9))
|
2026-03-25 18:39:36 +00:00
|
|
|
// Output:
|
|
|
|
|
// true
|
|
|
|
|
// false
|
|
|
|
|
// true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ExampleCore_RecordUsage() {
|
|
|
|
|
c := New()
|
|
|
|
|
var recorded string
|
|
|
|
|
c.SetUsageRecorder(func(action string, qty int, _ context.Context) {
|
feat: eliminate fmt, string concat — add core.Println, use Concat/Path everywhere
New primitive: core.Println() wraps fmt.Println.
Replaced across all test + example files:
- fmt.Println → Println (17 example files)
- fmt.Sprintf → Concat + Sprint
- dir + "/file" → Path(dir, "file") (path security)
- "str" + var → Concat("str", var) (AX consistency)
"fmt" import is now zero across all test files.
String concat with + is zero across all test files.
Remaining 9 stdlib imports (all Go infrastructure):
testing, context, time, sync, embed, io/fs, bytes, gzip, base64
558 tests, 84.5% coverage.
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-25 19:42:39 +00:00
|
|
|
recorded = Concat(action, ":", Sprint(qty))
|
2026-03-25 18:39:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
c.RecordUsage("ai.credits", 10)
|
feat: eliminate fmt, string concat — add core.Println, use Concat/Path everywhere
New primitive: core.Println() wraps fmt.Println.
Replaced across all test + example files:
- fmt.Println → Println (17 example files)
- fmt.Sprintf → Concat + Sprint
- dir + "/file" → Path(dir, "file") (path security)
- "str" + var → Concat("str", var) (AX consistency)
"fmt" import is now zero across all test files.
String concat with + is zero across all test files.
Remaining 9 stdlib imports (all Go infrastructure):
testing, context, time, sync, embed, io/fs, bytes, gzip, base64
558 tests, 84.5% coverage.
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-25 19:42:39 +00:00
|
|
|
Println(recorded)
|
2026-03-25 18:39:36 +00:00
|
|
|
// Output: ai.credits:10
|
|
|
|
|
}
|