How shiwano/master works
The project “master” is a command-line tool written in Go that converts CSV files into structured JSON while optionally validating the JSON against a JSON Schema. The design is aimed at a straightforward data transformation pipeline that begins with CSV input (with configurable file encoding), processes nested objects and arrays by interpreting dot-separated CSV header names, and then outputs JSON (or its corresponding JSON Schema) based on provided options.
Project’s Main Components
• Command-Line Interface (CLI) Module: This is represented by files such as cli.go (and its tests) and main.go, handling user commands, argument parsing, and program flow control.
• CSV Parsing Module: Files like csv.go and masterdata.go contain routines to parse CSV files — including handling of nested objects and arrays through the dot (.) notation.
• Encoding Handling Module: Implemented in encoding.go, this module utilizes the chardet library (and related logic) to detect and optionally fix CSV file encoding.
• JSON Conversion and Validation Module: Processes the parsed CSV data to generate JSON. It includes schema validation functions (found in validator.go) that apply JSON schema validation and, optionally, output JSON Schema definitions.
• Tests and Fixtures: The test files (e.g., cli_test.go, csv_test.go, masterdata_test.go, validator_test.go) and the fixtures directory (containing sample CSV, JSON, and schema files) help ensure the conversion logic works as intended.
• CLI as the “entry point”. This component reads command-line arguments and invokes the main application logic.
• CSV Parser: Receives the input CSV file, uses csv.go functions to parse and structure the data.
• Encoding Handler: Works in tandem with CSV Parser to check and convert CSV file encodings as needed.
• Flow from the CSV Parser to the JSON Conversion Engine, where the parsed CSV data is transformed into JSON format based on user options.
• A branch for JSON Validation:
• If the validation flag is on, direct the flow from the JSON Conversion Engine to the JSON Schema Validator module (from validator.go) which retrieves the JSON Schema file (exemplified by fixtures like masterdata.schema.json) and validates the conversion output.
• Finally, represent the Output Generator:
• This component either writes the JSON output to file (or a directory, as per the CLI option) or prints it to stdout if the no-output flag is enabled.
Architectural Patterns & Design Principles
• Separation of Concerns: Libraries are organized so that CSV parsing, encoding handling, and JSON conversion/validation are in their own modules.
• Pipeline Architecture: Illustrate the step-by-step data flow — from reading input (CSV) through transformation and validation to produce final JSON.
• Testability: Include a component showing how tests (unit tests in files with _test.go suffix) ensure each module functions independently and correctly.
• Modularity and Extensibility: Since the project supports options for schema output and encoding fixes, each module is designed to work independently and be easily extendable.
Relevant Technologies and Tools
• JSON Schema and Standard JSON: Note the input (CSV) and output formats and how JSON Schema is integrated for validation purposes.