Skip to content

Commit 6f003b4

Browse files
committed
feat: Initial commit
0 parents  commit 6f003b4

File tree

9 files changed

+713
-0
lines changed

9 files changed

+713
-0
lines changed

.gemini/GEMINI.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# **Gemini Project Guidelines**
2+
3+
This document outlines the development and build rules specific to this project, intended for the Gemini agent. All development must strictly adhere to these principles to ensure the project's long-term quality, security, and maintainability.
4+
5+
For a detailed history of development and key decisions, please refer to the Development Log.
6+
For future development plans and roadmap, please refer to the Development Plan.
7+
8+
## **I. Core Philosophy & Overarching Principles**
9+
10+
*(哲学と最優先事項)*
11+
12+
These are the absolute, non-negotiable principles that guide all other decisions.
13+
(これらは他のすべての決定を導く、絶対的で交渉の余地のない原則です。)
14+
15+
### **I-1. Security First Principle (セキュリティ第一原則)**
16+
17+
* Security is the highest priority, overriding all other considerations such as functionality or performance. All code, dependencies, and configurations must be reviewed for potential security vulnerabilities before being committed.
18+
* Never trust user input, including environment variables. All external inputs must be validated and sanitized to prevent injection attacks.
19+
* Sensitive information (API keys, credentials) must never be hardcoded or stored in insecure locations. Configuration files containing sensitive information must be stored with restrictive permissions (e.g., 0600 for files, 0700 for directories).
20+
21+
### **I-2. Testability First Principle (テスト容易性第一の原則)**
22+
23+
* Prioritize practical "testability" over theoretical "beauty" in design. If a design pattern makes unit testing significantly difficult (e.g., self-registration via init()), it is forbidden.
24+
* Always ask, "How will I test this code?" Code that cannot be answered should not be written. Easily testable code is inherently loosely coupled and easy to understand.
25+
26+
## **II. Design & Implementation Principles**
27+
28+
*(設計と実装の原則)*
29+
30+
These principles govern how code should be designed and written.
31+
(これらはコードをどのように設計し、書くべきかを規定する原則です。)
32+
33+
### **II-1. Explicit is Better than Implicit (暗黙的な挙動の禁止)**
34+
35+
* Forbid "magical" implementations like changing global state in init() or behavior that changes merely by importing a package.
36+
* Dependency Injection must always be done explicitly (e.g., via function arguments). This makes code behavior traceable and easy to mock in tests.
37+
38+
### **II-2. Code Style and Quality (コードのスタイルと品質)**
39+
40+
* Adhere to standard Go formatting (gofmt) and idiomatic Go practices.
41+
* Keep functions concise and focused on a single responsibility.
42+
* Identify and address the root cause of bugs, avoiding temporary or superficial fixes.
43+
44+
### **II-3. Dependency Management (依存関係の管理)**
45+
46+
* Use Go Modules for dependency management.
47+
* Run go mod tidy after adding or removing dependencies to ensure go.mod and go.sum are up-to-date.
48+
* Regularly scan project dependencies for known vulnerabilities using make vulncheck.
49+
50+
## **III. Development Process & Workflow**
51+
52+
*(開発プロセスとワークフロー)*
53+
54+
These rules define the mandatory process for all development activities.
55+
(これらはすべての開発活動における必須のプロセスを定義するルールです。)
56+
57+
### **III-1. Definition of "Major Refactoring" (「大規模改修」の定義)**
58+
59+
Any change that meets one or more of the following criteria is considered a "Major Refactoring" and requires prior approval of a detailed development plan, including impact analysis and testing strategy.
60+
61+
* **Changes to Initialization Logic**: Any modification involving init() functions, global variables, or package initialization order.
62+
* **Changes to Core Interfaces**: Modifying the signature of a central interface like Provider.
63+
* **Widespread Impact**: A change that requires modifications across three or more packages.
64+
* **Introduction of Cross-Cutting Concerns**: Adding or changing functionality that affects multiple components, such as authentication, logging, or caching.
65+
66+
### **III-2. Safe Refactoring Protocol (安全なリファクタリング規約)**
67+
68+
Even for changes not classified as "Major Refactoring," the following protocol must be strictly followed to prevent system-wide failure.
69+
70+
* **III-2.1.【Establish Baseline】**: Commit the stable, fully-tested state of the code before starting. Commit message: refactor: Start refactoring X.
71+
* **III-2.2.【Make Minimal Changes】**: Make the smallest possible incremental change (e.g., extract one function, add one variable).
72+
* **III-2.3.【Test Immediately】**: Run make test immediately after the change.
73+
* **On Success**: Commit the change immediately (feat: Introduce Y / refactor: Extract Z). Return to Step **III-2.2**.
74+
* **On Failure**: **Discard all changes immediately (git reset --hard).** Do not attempt to fix the test by modifying other code. A test failure is a critical signal that the **design approach is flawed** and must be re-evaluated from scratch.
75+
* **III-2.4.【Complete】**: Achieve the final goal through a series of small, safe, and tested commits.
76+
77+
### **III-3. Commit Message Conventions (コミットメッセージの規約)**
78+
79+
* Use the Conventional Commits specification (e.g., feat:, fix:, refactor:, docs:).
80+
* Explain *why* a change was made, not just *what* was changed.
81+
82+
### **III-4. Build and Linting (ビルドとリント)**
83+
84+
* Always use the provided Makefile for building, testing, and cleaning the project.
85+
* Ensure all code passes lint checks (make lint) before committing.
86+
87+
## **IV. Tooling & Operational Safety**
88+
89+
*(ツール利用と操作の安全性)*
90+
91+
These rules govern the safe use of development tools and file system operations to prevent irreversible errors.
92+
(これらは不可逆的なエラーを防ぐため、開発ツールとファイルシステム操作の安全な利用法を規定するルールです。)
93+
94+
### **IV-1. Safe Code Modification Protocol (安全なコード修正手順)**
95+
96+
* **Problem**: The code replacement tool can be unreliable and may corrupt files.
97+
* **Rule**: To prevent file corruption, all code modifications must follow a "diff-and-replace" workflow.
98+
* **Process**:
99+
* **IV-1.1.** Generate the complete, modified content of the target file into a **temporary file** (e.g., original_filename.new).
100+
* **IV-1.2.** Execute a diff command between the original file and the new temporary file to verify the changes are exactly as intended.
101+
* **IV-1.3.** Only after visual confirmation of the diff, replace the original file with the temporary file using a mv command.
102+
103+
### **IV-2. Safe File System Operations (安全なファイルシステム操作)**
104+
105+
* **Problem**: Accidental deletion of unintended files due to incorrect path context.
106+
* **Rule**: All commands that delete or modify files/directories (e.g., rm, mv) **must use absolute paths.**
107+
* **Reason**: This prevents catastrophic errors resulting from operating in an unexpected current working directory. Relative paths are strictly forbidden for destructive operations.
108+
109+
### **IV-3. Safe Git Commit Procedure (安全なGitコミット手順)**
110+
111+
* **Problem**: Complex commit messages can be misinterpreted by the shell, leading to incorrect or failed commits.
112+
* **Rule**: To prevent shell interpretation errors, commit messages **must be written to a temporary file first.**
113+
* **Process**:
114+
* **IV-3.1.** Write the full commit message (including subject, body, and any special characters) to a temporary file (e.g., .git/COMMIT_MSG).
115+
* **IV-3.2.** Use the git commit -F .git/COMMIT_MSG command to apply the message from the file. Direct command-line commits with -m are forbidden for multi-line or complex messages.
116+
117+
## **V. Documentation Principles**
118+
119+
*(ドキュメンテーションの原則)*
120+
121+
* **V-1. Language Policy**: The primary documentation will be in English (e.g., README.md). Other languages will be provided as auxiliary documentation with a language suffix (e.g., README.ja.md).
122+
* **V-2. Maintenance**: When a feature is changed or added, ensure all relevant documentation (README.md, CHANGELOG.md, etc.) is updated accordingly.
123+
124+
## **VI. Agent Operational Protocols (自己規律)**
125+
126+
To prevent critical errors observed during the development of v1.2.0, the following operational protocols are mandatory for all future interactions. These supersede any general instructions where they conflict.
127+
128+
### **VI-1. State-Aware Git Workflow (状態を意識したGitワークフロー)**
129+
- **Rule:** Before any state-changing Git command (`merge`, `push`, `tag`, `reset`, `branch`), the current state **must** be verified by running `git status && git branch` immediately prior to the operation.
130+
- **Reason:** To prevent operating on incorrect branches or assumptions about the repository's state.
131+
132+
### **VI-2. Strict Adherence to Safety Protocols (安全プロトコルの厳守)**
133+
- **Rule:** The "Safe Code Modification Protocol" (IV-1) and "Safe Git Commit Procedure" (IV-3) are not optional guidelines but **mandatory, non-negotiable procedures**. All multi-line commits or complex file modifications **must** use the temporary file and diff method.
134+
- **Reason:** To eliminate syntax/escaping errors and ensure user verification before changes are applied.
135+
136+
### **VI-3. Rigorous Verification Before Merging (マージ前の厳格な検証)**
137+
- **Rule:** A feature or fix is not considered "complete" until it has been functionally verified by the user. The standard workflow is: `Implement -> Build -> **Manual Test & User Confirmation** -> Commit -> Merge`. The "Manual Test & User Confirmation" step is mandatory and cannot be skipped.
138+
- **Reason:** To ensure the implemented code correctly solves the user's problem before it is integrated into the main branch.

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Code coverage profiles and other test artifacts
12+
*.out
13+
coverage.*
14+
*.coverprofile
15+
profile.cov
16+
17+
# Dependency directories
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work
22+
go.work.sum
23+
24+
# env file
25+
.env
26+
27+
# Editor/IDE
28+
.idea/
29+
.vscode/
30+
31+
# Build artifacts for scat
32+
/bin/
33+
34+
# macOS specific files
35+
.DS_Store
36+
37+
# External files
38+
.external/**
39+
40+
# Test data
41+
testdata/
42+
43+
# IDE
44+
.vscode/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 magifd2
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# ==============================================================================
2+
# Project Variables
3+
# ==============================================================================
4+
5+
# The name of the executable
6+
TARGET := json-to-sqlite
7+
8+
# Get the version from the latest git tag. Fallback to v0.0.0-dev if no tags.
9+
VERSION ?= $(shell git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0-dev")
10+
11+
# Go build flags
12+
# -s -w: to make smaller binaries
13+
# -X main.Version=$(VERSION): to embed the version string
14+
LDFLAGS := -ldflags="-s -w -X main.Version=$(VERSION)"
15+
16+
# Build output directory
17+
BUILD_DIR := ./bin
18+
19+
20+
# ==============================================================================
21+
# Main Targets
22+
# ==============================================================================
23+
24+
.PHONY: all
25+
all: build
26+
27+
.PHONY: build
28+
build: build-linux build-windows build-mac
29+
@echo "All platforms built successfully."
30+
31+
.PHONY: package
32+
package: build
33+
@echo "Packaging for distribution..."
34+
$(MAKE) package-linux
35+
$(MAKE) package-windows
36+
$(MAKE) package-mac
37+
@echo "All packages created in $(BUILD_DIR)"
38+
39+
.PHONY: clean
40+
clean:
41+
@echo "Cleaning up build artifacts..."
42+
@rm -rf $(BUILD_DIR)
43+
44+
# ==============================================================================
45+
# Platform-Specific Build Targets
46+
# ==============================================================================
47+
48+
.PHONY: build-linux
49+
build-linux:
50+
@echo "Building for Linux (amd64)..."
51+
@mkdir -p $(BUILD_DIR)/linux-amd64
52+
@GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/linux-amd64/$(TARGET) .
53+
54+
.PHONY: build-windows
55+
build-windows:
56+
@echo "Building for Windows (amd64)..."
57+
@mkdir -p $(BUILD_DIR)/windows-amd64
58+
@GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/windows-amd64/$(TARGET).exe .
59+
60+
# Note: Building a universal binary requires running on macOS with Xcode tools installed.
61+
.PHONY: build-mac
62+
build-mac:
63+
@echo "Building for macOS (Universal)..."
64+
@mkdir -p $(BUILD_DIR)/darwin-universal
65+
@GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(TARGET)-amd64 .
66+
@GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(TARGET)-arm64 .
67+
@lipo -create -output $(BUILD_DIR)/darwin-universal/$(TARGET) $(BUILD_DIR)/$(TARGET)-amd64 $(BUILD_DIR)/$(TARGET)-arm64
68+
@rm $(BUILD_DIR)/$(TARGET)-amd64 $(BUILD_DIR)/$(TARGET)-arm64
69+
70+
71+
# ==============================================================================
72+
# Packaging Targets
73+
# ==============================================================================
74+
75+
.PHONY: package-linux
76+
package-linux:
77+
@cd $(BUILD_DIR)/linux-amd64 && tar -czf ../$(TARGET)-$(VERSION)-linux-amd64.tar.gz $(TARGET)
78+
79+
.PHONY: package-windows
80+
package-windows:
81+
@cd $(BUILD_DIR)/windows-amd64 && zip -r ../$(TARGET)-$(VERSION)-windows-amd64.zip $(TARGET).exe
82+
83+
.PHONY: package-mac
84+
package-mac:
85+
@cd $(BUILD_DIR)/darwin-universal && tar -czf ../$(TARGET)-$(VERSION)-darwin-universal.tar.gz $(TARGET)

README.ja.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# json-to-sqlite
2+
3+
JSONデータをインテリジェントに解釈し、SQLiteデータベースに変換する柔軟なコマンドラインツールです。
4+
5+
このツールはテーブルスキーマを自動的に推論し、新しいカラムを追加してスキーマの変更を処理し、ファイルまたは標準入力のどちらからでもJSONを処理できるため、データ処理と永続化のための強力なユーティリティとなります。
6+
7+
## 主な機能
8+
9+
- **自動スキーマ推論**: JSONオブジェクトの構造からSQLiteのテーブルスキーマをインテリジェントに生成します。
10+
- **スキーマの自動更新**: 入力データに新しいフィールドが検出されると、新しいカラムを追加して既存のテーブルをシームレスに更新します。
11+
- **柔軟な入力**: ファイルから、または標準入力から直接パイプで渡されたJSONデータを読み取ります。
12+
- **データ型マッピング**: JSONの型を適切なSQLiteの型(`TEXT`, `REAL`, `INTEGER`)に自動的にマッピングします。型が競合した場合は安全のために`TEXT`が使用されます。
13+
- **ネストされたJSONの処理**: ネストされたJSONオブジェクトや配列を`TEXT`カラムにシリアライズして保存します。
14+
- **クロスプラットフォーム**: 付属の`Makefile`により、Windows, Linux, macOS(ユニバーサルバイナリ)向けにビルドできます。
15+
16+
## インストール
17+
18+
### リリースから
19+
お使いのOSに対応したコンパイル済みのバイナリを[リリースページ](https://github.com/magifd2/json-to-sqlite/releases)からダウンロードしてください。
20+
21+
### ソースからビルド
22+
ソースからビルドするには、GoとMakeがインストールされている必要があります。
23+
24+
```bash
25+
# 1. リポジトリをクローン
26+
git clone https://github.com/magifd2/json-to-sqlite.git
27+
cd json-to-sqlite
28+
29+
# 2. バイナリをビルド
30+
make build
31+
32+
# 実行ファイルは ./bin/<OS>-<ARCH>/ に生成されます
33+
# 例: ./bin/darwin-universal/json-to-sqlite
34+
```
35+
36+
## 使い方
37+
38+
以下のフラグが使用できます。
39+
40+
- `-o <パス>`: 出力先のSQLiteデータベースファイルを指定します。(デフォルト: `output.db`)
41+
- `-t <テーブル名>`: 作成または更新するテーブル名を指定します。(デフォルト: `data`)
42+
- `--version`: ツールのバージョン情報を表示します。
43+
44+
### 使用例
45+
46+
**1. JSONファイルを新しいデータベースに変換する:**
47+
```bash
48+
json-to-sqlite -o users.db -t users ./users.json
49+
```
50+
51+
**2. 他のコマンド(例: `curl`)からJSONデータをパイプで渡す:**
52+
```bash
53+
curl "https://api.example.com/data" | json-to-sqlite -o api_data.db -t records
54+
```
55+
56+
**3. 新しいカラムを持つ可能性のあるデータを既存のデータベースに追加する:**
57+
```bash
58+
# new_users.jsonに新しいフィールドがあれば、この2回目のコマンドで'users'テーブルに新しいカラムが追加されます
59+
json-to-sqlite -o users.db -t users ./new_users.json
60+
```
61+
62+
## 動作の詳細
63+
64+
### 型のマッピング
65+
JSONの型は以下のようにSQLiteの型にマッピングされます。
66+
- `string` -> `TEXT`
67+
- `number` -> `REAL`
68+
- `boolean` -> `INTEGER` (`true`は1, `false`は0)
69+
- `array` -> `TEXT` (JSON文字列として保存)
70+
- `object` -> `TEXT` (JSON文字列として保存)
71+
72+
複数のオブジェクトで同じキーが異なるデータ型を持つ場合、全てのデータを損失なく保存するために、カラムの型は`TEXT`に昇格されます。
73+
74+
## ライセンス
75+
76+
このプロジェクトは [MITライセンス](LICENSE) の下で公開されています。

0 commit comments

Comments
 (0)