Files
opencloud/pkg/jmap/export_test.go
Pascal Bleser 2883f04488 groupware: refactoring using function templates
* adds creating addressbooks, calendars, mailboxes

 * adds deleting mailbox, event, identity

 * adds modifying an email

 * introduce template functions for the Groupware API in templates.go,
   and use those in route function implementations whenever possible

 * add capability checking for mail, quota, blobs

 * adds Changes interface

 * adds JmapResponse interface
2026-04-30 10:51:45 +02:00

90 lines
1.8 KiB
Go

package jmap
// This is for functions that are only supposed to be visible in tests.
import (
"fmt"
"go/ast"
"go/token"
"iter"
"log"
"strings"
"golang.org/x/tools/go/packages"
)
func valuesOf(p *packages.Package) iter.Seq[*ast.ValueSpec] { //NOSONAR
return func(yield func(*ast.ValueSpec) bool) {
for _, syn := range p.Syntax {
for _, decl := range syn.Decls {
g, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, s := range g.Specs {
e, ok := s.(*ast.ValueSpec)
if !ok {
continue
}
if !yield(e) {
return
}
}
}
}
}
}
func parseConsts(pkgID string, suffix string, typeName string) (map[string]string, error) { //NOSONAR
result := map[string]string{}
{
cfg := &packages.Config{
Mode: packages.LoadSyntax,
Dir: ".",
Tests: false,
}
pkgs, err := packages.Load(cfg, ".")
if err != nil {
log.Fatal(err)
}
if packages.PrintErrors(pkgs) > 0 {
return nil, fmt.Errorf("failed to parse the package '%s'", pkgID)
}
for _, p := range pkgs {
if p.ID != pkgID {
continue
}
for v := range valuesOf(p) {
for i, ident := range v.Names {
if ident != nil && strings.HasSuffix(ident.Name, suffix) {
value := v.Values[i]
switch c := value.(type) {
case *ast.CallExpr:
switch f := c.Fun.(type) {
case *ast.Ident:
if f.Name == typeName {
switch a := c.Args[0].(type) {
case *ast.BasicLit:
if a.Kind == token.STRING {
result[ident.Name] = strings.Trim(a.Value, `"`)
}
}
}
}
}
}
}
}
}
}
return result, nil
}
func firstKey[K comparable, V any](m map[K]V) (K, bool) {
for k := range m {
return k, true
}
var zero K
return zero, false
}