Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115package main
import ( "bytes" "fmt" "go/ast" "go/format" "go/parser" "go/token" "reflect" "strconv" "strings")
// patchTypes gives merge-patch objects presence-aware fields. Responses keep// their ordinary Go types. Nested anonymous objects get the same semantics.func patchTypes(source string) (string, string, error) { files := token.NewFileSet() file, err := parser.ParseFile(files, "api.gen.go", source, parser.ParseComments) if err != nil { return "", "", err } var methods strings.Builder var extra []ast.Decl var rewrite func(string, *ast.StructType) rewrite = func(name string, object *ast.StructType) { var method strings.Builder fmt.Fprintf(&method, "func (p %s) MarshalJSON() ([]byte, error) {\n fields := map[string]any{}\n", name) for _, field := range object.Fields.List { if len(field.Names) != 1 || field.Tag == nil { continue } tags, _ := strconv.Unquote(field.Tag.Value) tag := reflect.StructTag(tags) jsonTag := strings.Split(tag.Get("json"), ",") key := jsonTag[0] member := field.Names[0].Name optional := strings.Contains(tag.Get("json"), ",omitempty") if !optional { fmt.Fprintf(&method, "fields[%q] = p.%s\n", key, member) continue } underlying := field.Type if pointer, ok := underlying.(*ast.StarExpr); ok { underlying = pointer.X } if nested, ok := underlying.(*ast.StructType); ok { nestedName := name + member rewrite(nestedName, nested) extra = append(extra, &ast.GenDecl{Tok: token.TYPE, Specs: []ast.Spec{&ast.TypeSpec{Name: ast.NewIdent(nestedName), Type: nested}}}) underlying = ast.NewIdent(nestedName) } wrapper := "PatchField" if tag.Get("nullable") == "true" { wrapper = "NullablePatchField" } field.Type = &ast.IndexExpr{X: ast.NewIdent(wrapper), Index: underlying} fmt.Fprintf(&method, "if p.%s.present { fields[%q] = p.%s }\n", member, key, member) } method.WriteString("return json.Marshal(fields)\n}\n") methods.WriteString(method.String()) } // Collect first: recursive methods must not be emitted inside another method. var objects []struct { name string object *ast.StructType } for _, decl := range file.Decls { declaration, ok := decl.(*ast.GenDecl) if !ok { continue } for _, spec := range declaration.Specs { typ, ok := spec.(*ast.TypeSpec) if !ok || !strings.HasSuffix(typ.Name.Name, "Patch") { continue } object, ok := typ.Type.(*ast.StructType) if ok { objects = append(objects, struct { name string object *ast.StructType }{typ.Name.Name, object}) } } } for _, object := range objects { rewrite(object.name, object.object) } file.Decls = append(file.Decls, extra...) var output bytes.Buffer if err := format.Node(&output, files, file); err != nil { return "", "", err } helper := `// Code generated by apigen. DO NOT EDIT.package protocolimport ("encoding/json"; "fmt")// PatchField leaves a field unchanged in its zero value. SetPatch includes it.// Only NullablePatchField supports ClearPatch.type PatchField[T any] struct { present bool; value T }func SetPatch[T any](value T) PatchField[T] { return PatchField[T]{true, value} }func (p PatchField[T]) MarshalJSON() ([]byte, error) { data, err := json.Marshal(p.value) if err == nil && string(data) == "null" { return nil, fmt.Errorf("nonnullable patch field cannot be null") } return data,err}type NullablePatchField[T any] struct { present bool; value *T }func SetNullablePatch[T any](value T) NullablePatchField[T] { return NullablePatchField[T]{true, &value} }func (p NullablePatchField[T]) MarshalJSON() ([]byte, error) { return json.Marshal(p.value) }func ClearPatch[T any]() NullablePatchField[T] { return NullablePatchField[T]{present:true} }` + methods.String() formatted, err := format.Source([]byte(helper)) return output.String(), string(formatted), err}