mirror of
https://github.com/therootcompany/golib.git
synced 2026-03-13 12:27:59 +00:00
Separate library from CLI concerns: - Add Resolver callback type with Decision/Response structs for all interactive decisions (map/struct, type name, tuple/list, shape unification, shape naming, name collision) - Move terminal I/O (Prompter) from library to cmd/jsonpaths - Add public API: New(), ParseFormat(), Generate(), AutoGenerate() - Add Format type with aliases (ts, py, json-paths, etc.) - Fix godoc comments to match exported function names - Update tests to use scriptedResolver instead of Prompter internals - Update doc.go and README with current API
57 lines
2.0 KiB
Go
57 lines
2.0 KiB
Go
// Package jsontypes infers type structure from JSON samples and generates
|
|
// type definitions in multiple output formats.
|
|
//
|
|
// Given a JSON value (object, array, or primitive), jsontypes walks the
|
|
// structure depth-first, detects maps vs structs, infers optional fields
|
|
// from multiple instances, and produces a flat path notation called
|
|
// "json-paths" that captures the full type tree:
|
|
//
|
|
// {Root}
|
|
// .users[]{User}
|
|
// .users[].id{int}
|
|
// .users[].name{string}
|
|
// .users[].email{string?}
|
|
//
|
|
// These paths can then be rendered into typed definitions for any target:
|
|
//
|
|
// - [GenerateGoStructs]: Go struct definitions with json tags
|
|
// - [GenerateTypeScript]: TypeScript interfaces
|
|
// - [GenerateJSDoc]: JSDoc @typedef annotations
|
|
// - [GenerateZod]: Zod validation schemas
|
|
// - [GeneratePython]: Python TypedDict classes
|
|
// - [GenerateSQL]: SQL CREATE TABLE with foreign key relationships
|
|
// - [GenerateJSONSchema]: JSON Schema (draft 2020-12)
|
|
// - [GenerateTypedef]: JSON Typedef (RFC 8927)
|
|
//
|
|
// # Quick start
|
|
//
|
|
// For non-interactive use (e.g., from an AI agent or script):
|
|
//
|
|
// import "encoding/json"
|
|
// import "github.com/therootcompany/golib/tools/jsontypes"
|
|
//
|
|
// var data any
|
|
// dec := json.NewDecoder(input)
|
|
// dec.UseNumber()
|
|
// dec.Decode(&data)
|
|
//
|
|
// a := jsontypes.New(jsontypes.AnalyzerConfig{})
|
|
// paths := jsontypes.FormatPaths(a.Analyze(".", data))
|
|
// fmt.Print(jsontypes.GenerateTypeScript(paths))
|
|
//
|
|
// Or use the one-shot API:
|
|
//
|
|
// out, _ := jsontypes.AutoGenerate(jsonBytes, jsontypes.Options{
|
|
// Format: jsontypes.FormatTypeScript,
|
|
// })
|
|
//
|
|
// # AI tool use
|
|
//
|
|
// This package is designed to be callable as an AI skill. Given a JSON
|
|
// API response, an agent can infer the complete type structure and emit
|
|
// ready-to-use type definitions — no schema file required. The json-paths
|
|
// intermediate format is both human-readable and machine-parseable,
|
|
// making it suitable for tool-use chains where an agent needs to
|
|
// understand an API's shape before generating code.
|
|
package jsontypes
|