{"modulePath":"github.com/c360studio/semstreams","version":"v1.0.0-beta.146","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package parser\n\nPackage parser provides utilities for parsing various data formats into structured maps for use with StreamKit message processing.\n\n### Overview {#hdr-Overview}\n\nThe parser package offers simple, focused parsers for common data formats. Each parser converts raw bytes into map\\[string]any for integration with GenericJSON messages or other StreamKit processors.\n\n### Supported Formats {#hdr-Supported_Formats}\n\n\\*\\*JSON Parser (Production Ready):\\*\\*\n\nParses JSON data into structured maps with full error handling and validation.\n\n\\*\\*CSV Parser (Placeholder - Not Production Ready):\\*\\*\n\nCurrent CSV implementation is a placeholder that always returns \"parsed\": false. This parser is not functional and should not be used in production deployments.\n\n### JSON Parser {#hdr-JSON_Parser}\n\nThe JSON parser provides reliable parsing with validation:\n\n\timport \"github.com/c360studio/semstreams/processor/parser\"\n\n\t// Parse JSON bytes\n\tdata := []byte(`{\"sensor\": \"temp-001\", \"value\": 23.5}`)\n\tresult, err := parser.ParseJSON(data)\n\tif err != nil {\n\t    // Handle parse error\n\t}\n\n\t// Access parsed data\n\tsensorID := result[\"sensor\"].(string)    // \"temp-001\"\n\tvalue := result[\"value\"].(float64)       // 23.5\n\n### Validation {#hdr-Validation}\n\nParsers include validation methods to check data format before parsing:\n\n\t// Validate before parsing\n\terr := parser.ValidateJSON(data)\n\tif err != nil {\n\t    // Invalid JSON format\n\t    return err\n\t}\n\n\t// Parse validated data\n\tresult, err := parser.ParseJSON(data)\n\n### JSON Parser Features {#hdr-JSON_Parser_Features}\n\n\\*\\*Type Handling:\\*\\*\n\nJSON values are parsed to Go types:\n\n  - JSON strings → string\n  - JSON numbers → float64\n  - JSON booleans → bool\n  - JSON null → nil\n  - JSON objects → map\\[string]any\n  - JSON arrays → \\[]any\n\n\\*\\*Error Handling:\\*\\*\n\nThe parser uses standard Go error handling:\n\n\tresult, err := parser.ParseJSON(invalidJSON)\n\tif err != nil {\n\t    // errors.Is(err, parser.ErrInvalidJSON) == true\n\t}\n\n\\*\\*Validation:\\*\\*\n\n\terr := parser.ValidateJSON(data)\n\t// Returns error if:\n\t//   - Data is not valid JSON\n\t//   - Data is empty\n\t//   - JSON is not an object (must be {...})\n\n### JSON Parser Examples {#hdr-JSON_Parser_Examples}\n\n\\*\\*Parse Simple Object:\\*\\*\n\n\tdata := []byte(`{\"name\": \"sensor-001\", \"active\": true}`)\n\tresult, err := parser.ParseJSON(data)\n\t// result: map[string]any{\n\t//   \"name\": \"sensor-001\",\n\t//   \"active\": true,\n\t// }\n\n\\*\\*Parse Nested Object:\\*\\*\n\n\tdata := []byte(`{\n\t  \"sensor\": \"temp-001\",\n\t  \"reading\": {\n\t    \"value\": 23.5,\n\t    \"unit\": \"celsius\"\n\t  }\n\t}`)\n\tresult, err := parser.ParseJSON(data)\n\t// Access nested data:\n\treading := result[\"reading\"].(map[string]any)\n\tvalue := reading[\"value\"].(float64)\n\n\\*\\*Parse Array Values:\\*\\*\n\n\tdata := []byte(`{\n\t  \"sensor\": \"multi-001\",\n\t  \"readings\": [21.5, 22.0, 23.5]\n\t}`)\n\tresult, err := parser.ParseJSON(data)\n\treadings := result[\"readings\"].([]any)\n\tfirstReading := readings[0].(float64)  // 21.5\n\n\\*\\*Validate Before Parsing:\\*\\*\n\n\tdata := []byte(`{\"sensor\": \"temp-001\"}`)\n\n\t// Validate first\n\tif err := parser.ValidateJSON(data); err != nil {\n\t    return fmt.Errorf(\"invalid JSON: %w\", err)\n\t}\n\n\t// Parse validated data\n\tresult, err := parser.ParseJSON(data)\n\n### CSV Parser Status {#hdr-CSV_Parser_Status}\n\n⚠️ \\*\\*IMPORTANT: CSV Parser is NOT Production Ready\\*\\*\n\nThe current CSV parser implementation is a placeholder:\n\n\tresult, err := parser.ParseCSV(data)\n\t// Always returns: map[string]any{\"parsed\": false}\n\n\\*\\*DO NOT USE\\*\\* the CSV parser in production deployments.\n\n### CSV Parser Roadmap {#hdr-CSV_Parser_Roadmap}\n\nFuture CSV parser implementation will support:\n\n  - Header row parsing for field names\n  - Type inference for columns\n  - Configurable delimiters (comma, tab, pipe)\n  - Quote handling and escaping\n  - Multi-line field support\n\nIf you need CSV parsing now, consider:\n\n  - Use a dedicated CSV processor in your application\n  - Pre-process CSV to JSON before ingestion\n  - Contribute a CSV parser implementation to StreamKit\n\n### Integration with GenericJSON {#hdr-Integration_with_GenericJSON}\n\nParsed results can be wrapped in GenericJSON messages:\n\n\timport (\n\t    \"github.com/c360studio/semstreams/message\"\n\t    \"github.com/c360studio/semstreams/processor/parser\"\n\t)\n\n\t// Parse raw JSON\n\tdata := []byte(`{\"sensor\": \"temp-001\", \"value\": 23.5}`)\n\tparsed, err := parser.ParseJSON(data)\n\tif err != nil {\n\t    return err\n\t}\n\n\t// Wrap in GenericJSON\n\tpayload := message.NewGenericJSON(parsed)\n\t// Ready for StreamKit processing\n\n### Error Types {#hdr-Error_Types}\n\nThe parser defines standard error variables for error checking:\n\n\tvar (\n\t    ErrInvalidJSON = errors.New(\"invalid JSON format\")\n\t    ErrEmptyData   = errors.New(\"empty data\")\n\t)\n\nUse with errors.Is():\n\n\tif errors.Is(err, parser.ErrInvalidJSON) {\n\t    // Handle invalid JSON specifically\n\t}\n\n### Performance {#hdr-Performance}\n\n\\*\\*JSON Parser:\\*\\*\n\n  - Uses encoding/json from standard library\n  - Performance: ~100,000 small objects/second\n  - Memory: Minimal allocations for simple objects\n  - Suitable for high-throughput message processing\n\n\\*\\*CSV Parser:\\*\\*\n\n  - Not applicable (placeholder implementation)\n\n### Testing {#hdr-Testing}\n\nThe package has 100% test coverage for JSON parsing:\n\n  - Valid JSON objects\n  - Invalid JSON formats\n  - Empty data handling\n  - Type preservation\n  - Error conditions\n\nRun tests:\n\n\tgo test ./processor/parser -v\n\n### Design Philosophy {#hdr-Design_Philosophy}\n\nThe parser package follows these principles:\n\n  - \\*\\*Simplicity\\*\\*: Focused on common use cases, not feature-complete\n  - \\*\\*Reliability\\*\\*: Production-ready features are fully tested\n  - \\*\\*Transparency\\*\\*: Placeholder features clearly documented as non-functional\n  - \\*\\*Integration\\*\\*: Output format designed for StreamKit message processing\n\n### Future Enhancements {#hdr-Future_Enhancements}\n\nPlanned additions based on user requirements:\n\n  - CSV parser implementation\n  - XML parser\n  - Protocol Buffers parser\n  - Custom delimiter support\n  - Schema validation\n  - Streaming parsers for large files\n\n### Migration Notes {#hdr-Migration_Notes}\n\nIf upgrading from a version expecting CSV support:\n\n  - Review all uses of parser.ParseCSV()\n  - Implement alternative CSV parsing\n  - Watch for parser package updates with CSV support\n  - Consider contributing CSV implementation\n\nFor questions or contributions, see the StreamKit repository.\n\n## Variables\n\n```go\nvar (\n\tErrInvalidFormat = errors.New(\"invalid data format\")\n\tErrEmptyData     = errors.New(\"empty data\")\n\tErrParsingFailed = errors.New(\"parsing failed\")\n)\n```\nCommon parsing errors\n\n## Types\n\n```go\ntype CSVParser struct{}\n```\nCSVParser handles CSV format data (placeholder implementation)\n\n```go\nfunc NewCSVParser() *CSVParser\n```\nNewCSVParser creates a new CSV parser\n\n```go\nfunc (p *CSVParser) Format() string\n```\nFormat returns the format name\n\n```go\nfunc (p *CSVParser) Parse(data []byte) (map[string]any, error)\n```\nParse parses CSV data into structured data\n\n```go\nfunc (p *CSVParser) Validate(data []byte) error\n```\nValidate checks if the data looks like CSV\n\n```go\ntype JSONParser struct{}\n```\nJSONParser handles JSON format data\n\n```go\nfunc NewJSONParser() *JSONParser\n```\nNewJSONParser creates a new JSON parser\n\n```go\nfunc (p *JSONParser) Format() string\n```\nFormat returns the format name\n\n```go\nfunc (p *JSONParser) Parse(data []byte) (map[string]any, error)\n```\nParse parses JSON data into a map\n\n```go\nfunc (p *JSONParser) Validate(data []byte) error\n```\nValidate checks if the data is valid JSON\n\n","path":"github.com/c360studio/semstreams/processor/parser","name":"parser","synopsis":"Package parser provides utilities for parsing various data formats into structured maps for use with StreamKit message processing.","isRedistributable":true}
