mirror of
https://gitee.com/johng/gf
synced 2026-06-07 18:26:02 +08:00
Compare commits
11 Commits
fix/gconv_
...
v1.16.8
| Author | SHA1 | Date | |
|---|---|---|---|
| 87067e1e8a | |||
| edf90799d3 | |||
| c8cfa33bc7 | |||
| 14ce35f570 | |||
| 56db028b21 | |||
| 561bc5bc33 | |||
| 79ba162156 | |||
| 03bcf51a95 | |||
| 308fdccf65 | |||
| fd92fd2409 | |||
| a2823a501e |
@ -1,21 +0,0 @@
|
||||
---
|
||||
name: "Standardize markdown document formatting"
|
||||
description: "Standardize the formatting of all markdown documents to keep structure clear, content readable, and the overall quality and user experience consistent. This document explains requirements for heading levels, paragraph formatting, code block usage, list formatting, and image and link insertion so authors can follow a unified style that is easier to read and maintain."
|
||||
applyTo: "*.{md,MD}"
|
||||
---
|
||||
|
||||
# Primary Formatting Requirements
|
||||
|
||||
- Keywords or specialized terms in the document must be formatted with inline code, for example `RuntimeClass`, `containerd`, `GPU`, and `AI`.
|
||||
- In Chinese text, do not add spaces around inline code.
|
||||
- For technical articles, review the generated content before finalizing it to ensure the material is technically accurate and contains no incorrect technical descriptions.
|
||||
- When the generated content is too large, split it into multiple tasks to avoid exceeding model output limits and causing the workflow to fail.
|
||||
|
||||
|
||||
# Detailed Content Requirements
|
||||
|
||||
- When documenting parameters or configuration items for a component or project, prefer tables when practical, and keep tables short enough to avoid horizontal scrolling during normal reading.
|
||||
- In Chinese paragraphs, use full-width punctuation rather than half-width punctuation, for example `,` instead of `,` and `;` instead of `;`.
|
||||
- Use `mermaid` for architecture diagrams, flowcharts, and similar visuals. If you need line breaks inside `mermaid`, use `<br/>` instead of `\n`.
|
||||
- If a code block is not a `mermaid` diagram and instead uses box-drawing characters such as `┌─`, `┐`, `┤`, or `│`, keep the content in English so the layout stays aligned.
|
||||
- Do not use `---` as a separator between paragraphs.
|
||||
@ -1,152 +0,0 @@
|
||||
---
|
||||
name: "OPSX: Apply"
|
||||
description: Implement tasks from an OpenSpec change (Experimental)
|
||||
category: Workflow
|
||||
tags: [workflow, artifacts, experimental]
|
||||
---
|
||||
|
||||
Implement tasks from an OpenSpec change.
|
||||
|
||||
**Input**: Optionally specify a change name (e.g., `/opsx:apply add-auth`). If omitted, check if it can be inferred from conversation context. If vague or ambiguous you MUST prompt for available changes.
|
||||
|
||||
**Steps**
|
||||
|
||||
1. **Select the change**
|
||||
|
||||
If a name is provided, use it. Otherwise:
|
||||
- Infer from conversation context if the user mentioned a change
|
||||
- Auto-select if only one active change exists
|
||||
- If ambiguous, run `openspec list --json` to get available changes and use the **AskUserQuestion tool** to let the user select
|
||||
|
||||
Always announce: "Using change: <name>" and how to override (e.g., `/opsx:apply <other>`).
|
||||
|
||||
2. **Check status to understand the schema**
|
||||
```bash
|
||||
openspec status --change "<name>" --json
|
||||
```
|
||||
Parse the JSON to understand:
|
||||
- `schemaName`: The workflow being used (e.g., "spec-driven")
|
||||
- Which artifact contains the tasks (typically "tasks" for spec-driven, check status for others)
|
||||
|
||||
3. **Get apply instructions**
|
||||
|
||||
```bash
|
||||
openspec instructions apply --change "<name>" --json
|
||||
```
|
||||
|
||||
This returns:
|
||||
- Context file paths (varies by schema)
|
||||
- Progress (total, complete, remaining)
|
||||
- Task list with status
|
||||
- Dynamic instruction based on current state
|
||||
|
||||
**Handle states:**
|
||||
- If `state: "blocked"` (missing artifacts): show message, suggest using `/opsx:continue`
|
||||
- If `state: "all_done"`: congratulate, suggest archive
|
||||
- Otherwise: proceed to implementation
|
||||
|
||||
4. **Read context files**
|
||||
|
||||
Read the files listed in `contextFiles` from the apply instructions output.
|
||||
The files depend on the schema being used:
|
||||
- **spec-driven**: proposal, specs, design, tasks
|
||||
- Other schemas: follow the contextFiles from CLI output
|
||||
|
||||
5. **Show current progress**
|
||||
|
||||
Display:
|
||||
- Schema being used
|
||||
- Progress: "N/M tasks complete"
|
||||
- Remaining tasks overview
|
||||
- Dynamic instruction from CLI
|
||||
|
||||
6. **Implement tasks (loop until done or blocked)**
|
||||
|
||||
For each pending task:
|
||||
- Show which task is being worked on
|
||||
- Make the code changes required
|
||||
- Keep changes minimal and focused
|
||||
- Mark task complete in the tasks file: `- [ ]` → `- [x]`
|
||||
- Continue to next task
|
||||
|
||||
**Pause if:**
|
||||
- Task is unclear → ask for clarification
|
||||
- Implementation reveals a design issue → suggest updating artifacts
|
||||
- Error or blocker encountered → report and wait for guidance
|
||||
- User interrupts
|
||||
|
||||
7. **On completion or pause, show status**
|
||||
|
||||
Display:
|
||||
- Tasks completed this session
|
||||
- Overall progress: "N/M tasks complete"
|
||||
- If all done: suggest archive
|
||||
- If paused: explain why and wait for guidance
|
||||
|
||||
**Output During Implementation**
|
||||
|
||||
```
|
||||
## Implementing: <change-name> (schema: <schema-name>)
|
||||
|
||||
Working on task 3/7: <task description>
|
||||
[...implementation happening...]
|
||||
✓ Task complete
|
||||
|
||||
Working on task 4/7: <task description>
|
||||
[...implementation happening...]
|
||||
✓ Task complete
|
||||
```
|
||||
|
||||
**Output On Completion**
|
||||
|
||||
```
|
||||
## Implementation Complete
|
||||
|
||||
**Change:** <change-name>
|
||||
**Schema:** <schema-name>
|
||||
**Progress:** 7/7 tasks complete ✓
|
||||
|
||||
### Completed This Session
|
||||
- [x] Task 1
|
||||
- [x] Task 2
|
||||
...
|
||||
|
||||
All tasks complete! You can archive this change with `/opsx:archive`.
|
||||
```
|
||||
|
||||
**Output On Pause (Issue Encountered)**
|
||||
|
||||
```
|
||||
## Implementation Paused
|
||||
|
||||
**Change:** <change-name>
|
||||
**Schema:** <schema-name>
|
||||
**Progress:** 4/7 tasks complete
|
||||
|
||||
### Issue Encountered
|
||||
<description of the issue>
|
||||
|
||||
**Options:**
|
||||
1. <option 1>
|
||||
2. <option 2>
|
||||
3. Other approach
|
||||
|
||||
What would you like to do?
|
||||
```
|
||||
|
||||
**Guardrails**
|
||||
- Keep going through tasks until done or blocked
|
||||
- Always read context files before starting (from the apply instructions output)
|
||||
- If task is ambiguous, pause and ask before implementing
|
||||
- If implementation reveals issues, pause and suggest artifact updates
|
||||
- Keep code changes minimal and scoped to each task
|
||||
- Update task checkbox immediately after completing each task
|
||||
- Pause on errors, blockers, or unclear requirements - don't guess
|
||||
- Use contextFiles from CLI output, don't assume specific file names
|
||||
|
||||
**Fluid Workflow Integration**
|
||||
|
||||
This skill supports the "actions on a change" model:
|
||||
|
||||
- **Can be invoked anytime**: Before all artifacts are done (if tasks exist), after partial implementation, interleaved with other actions
|
||||
- **Allows artifact updates**: If implementation reveals design issues, suggest updating artifacts - not phase-locked, work fluidly
|
||||
@ -1,157 +0,0 @@
|
||||
---
|
||||
name: "OPSX: Archive"
|
||||
description: Archive a completed change in the experimental workflow
|
||||
category: Workflow
|
||||
tags: [workflow, archive, experimental]
|
||||
---
|
||||
|
||||
Archive a completed change in the experimental workflow.
|
||||
|
||||
**Input**: Optionally specify a change name after `/opsx:archive` (e.g., `/opsx:archive add-auth`). If omitted, check if it can be inferred from conversation context. If vague or ambiguous you MUST prompt for available changes.
|
||||
|
||||
**Steps**
|
||||
|
||||
1. **If no change name provided, prompt for selection**
|
||||
|
||||
Run `openspec list --json` to get available changes. Use the **AskUserQuestion tool** to let the user select.
|
||||
|
||||
Show only active changes (not already archived).
|
||||
Include the schema used for each change if available.
|
||||
|
||||
**IMPORTANT**: Do NOT guess or auto-select a change. Always let the user choose.
|
||||
|
||||
2. **Check artifact completion status**
|
||||
|
||||
Run `openspec status --change "<name>" --json` to check artifact completion.
|
||||
|
||||
Parse the JSON to understand:
|
||||
- `schemaName`: The workflow being used
|
||||
- `artifacts`: List of artifacts with their status (`done` or other)
|
||||
|
||||
**If any artifacts are not `done`:**
|
||||
- Display warning listing incomplete artifacts
|
||||
- Prompt user for confirmation to continue
|
||||
- Proceed if user confirms
|
||||
|
||||
3. **Check task completion status**
|
||||
|
||||
Read the tasks file (typically `tasks.md`) to check for incomplete tasks.
|
||||
|
||||
Count tasks marked with `- [ ]` (incomplete) vs `- [x]` (complete).
|
||||
|
||||
**If incomplete tasks found:**
|
||||
- Display warning showing count of incomplete tasks
|
||||
- Prompt user for confirmation to continue
|
||||
- Proceed if user confirms
|
||||
|
||||
**If no tasks file exists:** Proceed without task-related warning.
|
||||
|
||||
4. **Assess delta spec sync state**
|
||||
|
||||
Check for delta specs at `openspec/changes/<name>/specs/`. If none exist, proceed without sync prompt.
|
||||
|
||||
**If delta specs exist:**
|
||||
- Compare each delta spec with its corresponding main spec at `openspec/specs/<capability>/spec.md`
|
||||
- Determine what changes would be applied (adds, modifications, removals, renames)
|
||||
- Show a combined summary before prompting
|
||||
|
||||
**Prompt options:**
|
||||
- If changes needed: "Sync now (recommended)", "Archive without syncing"
|
||||
- If already synced: "Archive now", "Sync anyway", "Cancel"
|
||||
|
||||
If user chooses sync, use Task tool (subagent_type: "general-purpose", prompt: "Use Skill tool to invoke openspec-sync-specs for change '<name>'. Delta spec analysis: <include the analyzed delta spec summary>"). Proceed to archive regardless of choice.
|
||||
|
||||
5. **Perform the archive**
|
||||
|
||||
Create the archive directory if it doesn't exist:
|
||||
```bash
|
||||
mkdir -p openspec/changes/archive
|
||||
```
|
||||
|
||||
Generate target name using current date: `YYYY-MM-DD-<change-name>`
|
||||
|
||||
**Check if target already exists:**
|
||||
- If yes: Fail with error, suggest renaming existing archive or using different date
|
||||
- If no: Move the change directory to archive
|
||||
|
||||
```bash
|
||||
mv openspec/changes/<name> openspec/changes/archive/YYYY-MM-DD-<name>
|
||||
```
|
||||
|
||||
6. **Display summary**
|
||||
|
||||
Show archive completion summary including:
|
||||
- Change name
|
||||
- Schema that was used
|
||||
- Archive location
|
||||
- Spec sync status (synced / sync skipped / no delta specs)
|
||||
- Note about any warnings (incomplete artifacts/tasks)
|
||||
|
||||
**Output On Success**
|
||||
|
||||
```
|
||||
## Archive Complete
|
||||
|
||||
**Change:** <change-name>
|
||||
**Schema:** <schema-name>
|
||||
**Archived to:** openspec/changes/archive/YYYY-MM-DD-<name>/
|
||||
**Specs:** ✓ Synced to main specs
|
||||
|
||||
All artifacts complete. All tasks complete.
|
||||
```
|
||||
|
||||
**Output On Success (No Delta Specs)**
|
||||
|
||||
```
|
||||
## Archive Complete
|
||||
|
||||
**Change:** <change-name>
|
||||
**Schema:** <schema-name>
|
||||
**Archived to:** openspec/changes/archive/YYYY-MM-DD-<name>/
|
||||
**Specs:** No delta specs
|
||||
|
||||
All artifacts complete. All tasks complete.
|
||||
```
|
||||
|
||||
**Output On Success With Warnings**
|
||||
|
||||
```
|
||||
## Archive Complete (with warnings)
|
||||
|
||||
**Change:** <change-name>
|
||||
**Schema:** <schema-name>
|
||||
**Archived to:** openspec/changes/archive/YYYY-MM-DD-<name>/
|
||||
**Specs:** Sync skipped (user chose to skip)
|
||||
|
||||
**Warnings:**
|
||||
- Archived with 2 incomplete artifacts
|
||||
- Archived with 3 incomplete tasks
|
||||
- Delta spec sync was skipped (user chose to skip)
|
||||
|
||||
Review the archive if this was not intentional.
|
||||
```
|
||||
|
||||
**Output On Error (Archive Exists)**
|
||||
|
||||
```
|
||||
## Archive Failed
|
||||
|
||||
**Change:** <change-name>
|
||||
**Target:** openspec/changes/archive/YYYY-MM-DD-<name>/
|
||||
|
||||
Target archive directory already exists.
|
||||
|
||||
**Options:**
|
||||
1. Rename the existing archive
|
||||
2. Delete the existing archive if it's a duplicate
|
||||
3. Wait until a different date to archive
|
||||
```
|
||||
|
||||
**Guardrails**
|
||||
- Always prompt for change selection if not provided
|
||||
- Use artifact graph (openspec status --json) for completion checking
|
||||
- Don't block archive on warnings - just inform and confirm
|
||||
- Preserve .openspec.yaml when moving to archive (it moves with the directory)
|
||||
- Show clear summary of what happened
|
||||
- If sync is requested, use the Skill tool to invoke `openspec-sync-specs` (agent-driven)
|
||||
- If delta specs exist, always run the sync assessment and show the combined summary before prompting
|
||||
@ -1,173 +0,0 @@
|
||||
---
|
||||
name: "OPSX: Explore"
|
||||
description: "Enter explore mode - think through ideas, investigate problems, clarify requirements"
|
||||
category: Workflow
|
||||
tags: [workflow, explore, experimental, thinking]
|
||||
---
|
||||
|
||||
Enter explore mode. Think deeply. Visualize freely. Follow the conversation wherever it goes.
|
||||
|
||||
**IMPORTANT: Explore mode is for thinking, not implementing.** You may read files, search code, and investigate the codebase, but you must NEVER write code or implement features. If the user asks you to implement something, remind them to exit explore mode first and create a change proposal. You MAY create OpenSpec artifacts (proposals, designs, specs) if the user asks—that's capturing thinking, not implementing.
|
||||
|
||||
**This is a stance, not a workflow.** There are no fixed steps, no required sequence, no mandatory outputs. You're a thinking partner helping the user explore.
|
||||
|
||||
**Input**: The argument after `/opsx:explore` is whatever the user wants to think about. Could be:
|
||||
- A vague idea: "real-time collaboration"
|
||||
- A specific problem: "the auth system is getting unwieldy"
|
||||
- A change name: "add-dark-mode" (to explore in context of that change)
|
||||
- A comparison: "postgres vs sqlite for this"
|
||||
- Nothing (just enter explore mode)
|
||||
|
||||
---
|
||||
|
||||
## The Stance
|
||||
|
||||
- **Curious, not prescriptive** - Ask questions that emerge naturally, don't follow a script
|
||||
- **Open threads, not interrogations** - Surface multiple interesting directions and let the user follow what resonates. Don't funnel them through a single path of questions.
|
||||
- **Visual** - Use ASCII diagrams liberally when they'd help clarify thinking
|
||||
- **Adaptive** - Follow interesting threads, pivot when new information emerges
|
||||
- **Patient** - Don't rush to conclusions, let the shape of the problem emerge
|
||||
- **Grounded** - Explore the actual codebase when relevant, don't just theorize
|
||||
|
||||
---
|
||||
|
||||
## What You Might Do
|
||||
|
||||
Depending on what the user brings, you might:
|
||||
|
||||
**Explore the problem space**
|
||||
- Ask clarifying questions that emerge from what they said
|
||||
- Challenge assumptions
|
||||
- Reframe the problem
|
||||
- Find analogies
|
||||
|
||||
**Investigate the codebase**
|
||||
- Map existing architecture relevant to the discussion
|
||||
- Find integration points
|
||||
- Identify patterns already in use
|
||||
- Surface hidden complexity
|
||||
|
||||
**Compare options**
|
||||
- Brainstorm multiple approaches
|
||||
- Build comparison tables
|
||||
- Sketch tradeoffs
|
||||
- Recommend a path (if asked)
|
||||
|
||||
**Visualize**
|
||||
```
|
||||
┌─────────────────────────────────────────┐
|
||||
│ Use ASCII diagrams liberally │
|
||||
├─────────────────────────────────────────┤
|
||||
│ │
|
||||
│ ┌────────┐ ┌────────┐ │
|
||||
│ │ State │────────▶│ State │ │
|
||||
│ │ A │ │ B │ │
|
||||
│ └────────┘ └────────┘ │
|
||||
│ │
|
||||
│ System diagrams, state machines, │
|
||||
│ data flows, architecture sketches, │
|
||||
│ dependency graphs, comparison tables │
|
||||
│ │
|
||||
└─────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
**Surface risks and unknowns**
|
||||
- Identify what could go wrong
|
||||
- Find gaps in understanding
|
||||
- Suggest spikes or investigations
|
||||
|
||||
---
|
||||
|
||||
## OpenSpec Awareness
|
||||
|
||||
You have full context of the OpenSpec system. Use it naturally, don't force it.
|
||||
|
||||
### Check for context
|
||||
|
||||
At the start, quickly check what exists:
|
||||
```bash
|
||||
openspec list --json
|
||||
```
|
||||
|
||||
This tells you:
|
||||
- If there are active changes
|
||||
- Their names, schemas, and status
|
||||
- What the user might be working on
|
||||
|
||||
If the user mentioned a specific change name, read its artifacts for context.
|
||||
|
||||
### When no change exists
|
||||
|
||||
Think freely. When insights crystallize, you might offer:
|
||||
|
||||
- "This feels solid enough to start a change. Want me to create a proposal?"
|
||||
- Or keep exploring - no pressure to formalize
|
||||
|
||||
### When a change exists
|
||||
|
||||
If the user mentions a change or you detect one is relevant:
|
||||
|
||||
1. **Read existing artifacts for context**
|
||||
- `openspec/changes/<name>/proposal.md`
|
||||
- `openspec/changes/<name>/design.md`
|
||||
- `openspec/changes/<name>/tasks.md`
|
||||
- etc.
|
||||
|
||||
2. **Reference them naturally in conversation**
|
||||
- "Your design mentions using Redis, but we just realized SQLite fits better..."
|
||||
- "The proposal scopes this to premium users, but we're now thinking everyone..."
|
||||
|
||||
3. **Offer to capture when decisions are made**
|
||||
|
||||
| Insight Type | Where to Capture |
|
||||
|--------------|------------------|
|
||||
| New requirement discovered | `specs/<capability>/spec.md` |
|
||||
| Requirement changed | `specs/<capability>/spec.md` |
|
||||
| Design decision made | `design.md` |
|
||||
| Scope changed | `proposal.md` |
|
||||
| New work identified | `tasks.md` |
|
||||
| Assumption invalidated | Relevant artifact |
|
||||
|
||||
Example offers:
|
||||
- "That's a design decision. Capture it in design.md?"
|
||||
- "This is a new requirement. Add it to specs?"
|
||||
- "This changes scope. Update the proposal?"
|
||||
|
||||
4. **The user decides** - Offer and move on. Don't pressure. Don't auto-capture.
|
||||
|
||||
---
|
||||
|
||||
## What You Don't Have To Do
|
||||
|
||||
- Follow a script
|
||||
- Ask the same questions every time
|
||||
- Produce a specific artifact
|
||||
- Reach a conclusion
|
||||
- Stay on topic if a tangent is valuable
|
||||
- Be brief (this is thinking time)
|
||||
|
||||
---
|
||||
|
||||
## Ending Discovery
|
||||
|
||||
There's no required ending. Discovery might:
|
||||
|
||||
- **Flow into a proposal**: "Ready to start? I can create a change proposal."
|
||||
- **Result in artifact updates**: "Updated design.md with these decisions"
|
||||
- **Just provide clarity**: User has what they need, moves on
|
||||
- **Continue later**: "We can pick this up anytime"
|
||||
|
||||
When things crystallize, you might offer a summary - but it's optional. Sometimes the thinking IS the value.
|
||||
|
||||
---
|
||||
|
||||
## Guardrails
|
||||
|
||||
- **Don't implement** - Never write code or implement features. Creating OpenSpec artifacts is fine, writing application code is not.
|
||||
- **Don't fake understanding** - If something is unclear, dig deeper
|
||||
- **Don't rush** - Discovery is thinking time, not task time
|
||||
- **Don't force structure** - Let patterns emerge naturally
|
||||
- **Don't auto-capture** - Offer to save insights, don't just do it
|
||||
- **Do visualize** - A good diagram is worth many paragraphs
|
||||
- **Do explore the codebase** - Ground discussions in reality
|
||||
- **Do question assumptions** - Including the user's and your own
|
||||
@ -1,106 +0,0 @@
|
||||
---
|
||||
name: "OPSX: Propose"
|
||||
description: Propose a new change - create it and generate all artifacts in one step
|
||||
category: Workflow
|
||||
tags: [workflow, artifacts, experimental]
|
||||
---
|
||||
|
||||
Propose a new change - create the change and generate all artifacts in one step.
|
||||
|
||||
I'll create a change with artifacts:
|
||||
- proposal.md (what & why)
|
||||
- design.md (how)
|
||||
- tasks.md (implementation steps)
|
||||
|
||||
When ready to implement, run /opsx:apply
|
||||
|
||||
---
|
||||
|
||||
**Input**: The argument after `/opsx:propose` is the change name (kebab-case), OR a description of what the user wants to build.
|
||||
|
||||
**Steps**
|
||||
|
||||
1. **If no input provided, ask what they want to build**
|
||||
|
||||
Use the **AskUserQuestion tool** (open-ended, no preset options) to ask:
|
||||
> "What change do you want to work on? Describe what you want to build or fix."
|
||||
|
||||
From their description, derive a kebab-case name (e.g., "add user authentication" → `add-user-auth`).
|
||||
|
||||
**IMPORTANT**: Do NOT proceed without understanding what the user wants to build.
|
||||
|
||||
2. **Create the change directory**
|
||||
```bash
|
||||
openspec new change "<name>"
|
||||
```
|
||||
This creates a scaffolded change at `openspec/changes/<name>/` with `.openspec.yaml`.
|
||||
|
||||
3. **Get the artifact build order**
|
||||
```bash
|
||||
openspec status --change "<name>" --json
|
||||
```
|
||||
Parse the JSON to get:
|
||||
- `applyRequires`: array of artifact IDs needed before implementation (e.g., `["tasks"]`)
|
||||
- `artifacts`: list of all artifacts with their status and dependencies
|
||||
|
||||
4. **Create artifacts in sequence until apply-ready**
|
||||
|
||||
Use the **TodoWrite tool** to track progress through the artifacts.
|
||||
|
||||
Loop through artifacts in dependency order (artifacts with no pending dependencies first):
|
||||
|
||||
a. **For each artifact that is `ready` (dependencies satisfied)**:
|
||||
- Get instructions:
|
||||
```bash
|
||||
openspec instructions <artifact-id> --change "<name>" --json
|
||||
```
|
||||
- The instructions JSON includes:
|
||||
- `context`: Project background (constraints for you - do NOT include in output)
|
||||
- `rules`: Artifact-specific rules (constraints for you - do NOT include in output)
|
||||
- `template`: The structure to use for your output file
|
||||
- `instruction`: Schema-specific guidance for this artifact type
|
||||
- `outputPath`: Where to write the artifact
|
||||
- `dependencies`: Completed artifacts to read for context
|
||||
- Read any completed dependency files for context
|
||||
- Create the artifact file using `template` as the structure
|
||||
- Apply `context` and `rules` as constraints - but do NOT copy them into the file
|
||||
- Show brief progress: "Created <artifact-id>"
|
||||
|
||||
b. **Continue until all `applyRequires` artifacts are complete**
|
||||
- After creating each artifact, re-run `openspec status --change "<name>" --json`
|
||||
- Check if every artifact ID in `applyRequires` has `status: "done"` in the artifacts array
|
||||
- Stop when all `applyRequires` artifacts are done
|
||||
|
||||
c. **If an artifact requires user input** (unclear context):
|
||||
- Use **AskUserQuestion tool** to clarify
|
||||
- Then continue with creation
|
||||
|
||||
5. **Show final status**
|
||||
```bash
|
||||
openspec status --change "<name>"
|
||||
```
|
||||
|
||||
**Output**
|
||||
|
||||
After completing all artifacts, summarize:
|
||||
- Change name and location
|
||||
- List of artifacts created with brief descriptions
|
||||
- What's ready: "All artifacts created! Ready for implementation."
|
||||
- Prompt: "Run `/opsx:apply` to start implementing."
|
||||
|
||||
**Artifact Creation Guidelines**
|
||||
|
||||
- Follow the `instruction` field from `openspec instructions` for each artifact type
|
||||
- The schema defines what each artifact should contain - follow it
|
||||
- Read dependency artifacts for context before creating new ones
|
||||
- Use `template` as the structure for your output file - fill in its sections
|
||||
- **IMPORTANT**: `context` and `rules` are constraints for YOU, not content for the file
|
||||
- Do NOT copy `<context>`, `<rules>`, `<project_context>` blocks into the artifact
|
||||
- These guide what you write, but should never appear in the output
|
||||
|
||||
**Guardrails**
|
||||
- Create ALL artifacts needed for implementation (as defined by schema's `apply.requires`)
|
||||
- Always read dependency artifacts before creating a new one
|
||||
- If context is critically unclear, ask the user - but prefer making reasonable decisions to keep momentum
|
||||
- If a change with that name already exists, ask if user wants to continue it or create a new one
|
||||
- Verify each artifact file exists after writing before proceeding to next
|
||||
@ -1,280 +0,0 @@
|
||||
---
|
||||
name: gf-feedback
|
||||
description: >-
|
||||
Track, fix, verify, and test any bugs, improvements, or gaps reported against an OpenSpec change.
|
||||
MUST use this skill whenever user reports problems, defects, issues, bugs, or gaps related to
|
||||
existing implementations, even if they don't explicitly say "feedback" or mention OpenSpec.
|
||||
compatibility: Requires openspec CLI, Go toolchain, and gf-review skill.
|
||||
---
|
||||
|
||||
# Feedback: Structured Fix, Verification & Test Coverage Loop
|
||||
|
||||
When users discover bugs or improvement points after implementation, this skill captures those issues, organizes them into a traceable task list in `tasks.md`, systematically fixes and verifies each one, and ensures every behavior-changing fix is covered by focused unit tests.
|
||||
|
||||
**Core principles:**
|
||||
1. **Spec is the source of truth** — Spec-level changes require spec update before task recording
|
||||
2. **Write it down first, then fix it** — Every issue gets recorded before any code change
|
||||
3. **Every fix deserves a test** — Behavior-changing code changes require unit test coverage in the affected package
|
||||
|
||||
---
|
||||
|
||||
## Workflow
|
||||
|
||||
### 1. Identify Target Change
|
||||
|
||||
**CRITICAL:**
|
||||
1. Always append to existing active changes. Only create new change when none exist.
|
||||
2. An **active change** is any change directory that still exists directly under `openspec/changes/` and has **not** been moved into `openspec/changes/archive/`. Do **not** treat `status: complete`, all tasks checked off, or similar completion signals as "inactive" until archive actually happens.
|
||||
3. Regardless of whether the feedback content is related to the main functionality of the current active iteration, it MUST be appended to the current active iteration. This ensures all changes are tracked in a single change record for unified management and archiving.
|
||||
|
||||
```bash
|
||||
openspec list --json
|
||||
# Or: ls openspec/changes/ | grep -v archive
|
||||
```
|
||||
|
||||
When the two signals disagree, prefer the filesystem rule:
|
||||
|
||||
- If a change directory still exists under `openspec/changes/` and is not inside `archive/`, it is active.
|
||||
- `openspec list --json` may still report such a change as `status: complete`; that only means implementation tasks are done, **not** that the change is inactive.
|
||||
- Only archived changes under `openspec/changes/archive/` are inactive.
|
||||
|
||||
| Active Changes | Action |
|
||||
|----------------|--------|
|
||||
| None | Create new change (see below) |
|
||||
| One | Auto-select it, announce and proceed |
|
||||
| Multiple | Ask user to select from list |
|
||||
|
||||
**When multiple active changes exist:**
|
||||
```
|
||||
Multiple active changes detected. Which change should this feedback be appended to?
|
||||
|
||||
1. config-management — System config CRUD management
|
||||
2. user-auth — User authentication enhancement
|
||||
|
||||
Please select 1 or 2:
|
||||
```
|
||||
|
||||
**When no active change exists:**
|
||||
1. Derive kebab-case name from feedback (e.g., "fix-menu-circular-ref")
|
||||
2. If name exists, append suffix ("-2")
|
||||
3. Create: `openspec new change "<name>"`
|
||||
4. Generate minimal `proposal.md` (one paragraph summarizing context)
|
||||
5. Skip `design.md` for pure bug fixes unless architectural changes needed
|
||||
|
||||
Announce: "Applying feedback fixes to change: **<name>**"
|
||||
|
||||
---
|
||||
|
||||
### 2. Read Current Context
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `tasks.md` | Task structure, naming conventions, numbering |
|
||||
| `design.md` | Architectural context |
|
||||
| `proposal.md` | Feature scope and intent |
|
||||
| `specs/` | Delta spec definitions |
|
||||
|
||||
```bash
|
||||
# Locate existing unit tests in the impacted package before adding a new one
|
||||
rg --files <pkg-dir> | rg '_test\\.go$'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. Analyze and Organize Issues
|
||||
|
||||
For each reported issue:
|
||||
|
||||
**Classify by type:**
|
||||
- **bug** — Incorrect behavior, code doesn't match spec
|
||||
- **missing** — Feature incomplete, gaps in implementation
|
||||
- **ux** — UX improvement, no spec change needed
|
||||
- **test-gap** — Missing test coverage only
|
||||
|
||||
**Classify by spec impact:**
|
||||
|
||||
| Level | Definition | Action |
|
||||
|-------|------------|--------|
|
||||
| **implementation** | Spec is correct, code is wrong | Fix code only |
|
||||
| **spec-level** | Requirement missing/incomplete/changed | Update spec first, then fix |
|
||||
| **internal** | No user-observable change | Fix code, test optional |
|
||||
|
||||
**Group related issues** — Same root cause → single task with multiple verification points.
|
||||
|
||||
---
|
||||
|
||||
### 4. Update Delta Specs (for Spec-Level Issues Only)
|
||||
|
||||
For spec-level issues, update specs **before** recording tasks:
|
||||
|
||||
1. Identify affected capability: `specs/<capability>/spec.md`
|
||||
2. Apply delta operation:
|
||||
|
||||
```markdown
|
||||
<!-- ADDED: New requirement -->
|
||||
### Requirement: Parent Selector Circular Prevention
|
||||
The system SHALL disable the current menu and all its descendants in the parent selector
|
||||
to prevent circular references.
|
||||
|
||||
#### Scenario: Edit menu with children
|
||||
WHEN user edits a menu that has child menus
|
||||
THEN the parent selector SHALL disable the current menu and all descendant menus
|
||||
|
||||
<!-- MODIFIED: Changed requirement (include full original block) -->
|
||||
### Requirement: Import Error Handling
|
||||
The system SHALL display error messages when import fails.
|
||||
**MODIFIED:** Error messages SHALL include row number, field name, and validation failure reason.
|
||||
|
||||
<!-- REMOVED: Deprecated requirement -->
|
||||
### Requirement: Legacy Import Format
|
||||
The system SHALL support legacy CSV format.
|
||||
**REMOVED:** This format is no longer supported.
|
||||
**Migration:** Use the new CSV format with header row.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. Write Task List to tasks.md
|
||||
|
||||
Append a **Feedback section** to `tasks.md`:
|
||||
|
||||
```markdown
|
||||
## Feedback
|
||||
|
||||
- [ ] **FB-1**: Parent selector allows circular references in menu edit
|
||||
- [ ] **FB-2**: Import error messages lack row and field details
|
||||
- [ ] **FB-3**: No test coverage for reset password feature
|
||||
```
|
||||
|
||||
**Numbering:** Sequential `FB-1`, `FB-2`, etc. Continue from last number if section exists.
|
||||
|
||||
**One line per task** — No sub-fields. Analysis happens during fix phase.
|
||||
|
||||
**Confirm with user** before writing to file.
|
||||
|
||||
**Test coverage planning (internal):**
|
||||
- Behavior-changing code change → Unit test required
|
||||
- Internal-only optimization → Unit test optional unless logic risk increased
|
||||
- Prefer extending the nearest `*_z_unit*_test.go` or `*_test.go` in the same package
|
||||
|
||||
---
|
||||
|
||||
### 6. Execute Fixes (Loop)
|
||||
|
||||
For each task:
|
||||
|
||||
**a. Announce:** `## Fixing FB-X: <issue title>`
|
||||
|
||||
**b. Investigate** — Read source files, confirm root cause
|
||||
|
||||
**c. Implement** — Minimal, focused fix following existing patterns
|
||||
|
||||
**d. Write/update unit tests** — Prefer the affected package's existing `*_z_unit*_test.go` or `*_test.go` files and keep assertions focused on the changed logic
|
||||
|
||||
**e. Assess Impact Scope (MANDATORY)**
|
||||
|
||||
After implementing, identify regression risk:
|
||||
|
||||
| Change Type | Map To Tests |
|
||||
|-------------|--------------|
|
||||
| Package-level logic | Targeted test for changed function/method + package regression tests |
|
||||
| Shared utility | Utility package unit tests + highest-value dependent package tests already covering reuse |
|
||||
| DB/DAO logic | DAO/model package unit tests with focused fixtures, mocks, or test helpers |
|
||||
| Public API validation | Handler/service package unit tests that assert the changed validation path |
|
||||
| Refactor without behavior change | Existing package tests that prove behavior parity |
|
||||
|
||||
```bash
|
||||
# Example: Find unit tests related to a changed symbol or package
|
||||
rg -l "GenDao|gdao" . -g '*_test.go'
|
||||
```
|
||||
|
||||
Announce:
|
||||
```
|
||||
### Impact Analysis for FB-X
|
||||
- Modified: cmd/gf/internal/cmd/gendao/gendao.go
|
||||
- Affected package: cmd/gf/internal/cmd/gendao
|
||||
- Unit tests: cmd/gf/internal/cmd/gendao/gendao_test.go
|
||||
- Regression command: go test ./cmd/gf/internal/cmd/gendao -run 'TestGenDao'
|
||||
```
|
||||
|
||||
**f. Verify (MANDATORY before marking complete)**
|
||||
|
||||
1. Run new/updated unit tests for this task → **must pass**
|
||||
2. Run ALL identified package-level regression tests → **must pass**
|
||||
3. Only then: mark task `[x]` in tasks.md
|
||||
|
||||
If regression fails:
|
||||
- Fix inline if related to current change
|
||||
- Add as new FB task if separate issue
|
||||
|
||||
**g. Run review** — Invoke `gf-review` skill after completion
|
||||
|
||||
---
|
||||
|
||||
### 7. Comprehensive Verification
|
||||
|
||||
After all fixes:
|
||||
|
||||
1. Aggregate regression tests from all tasks
|
||||
2. Run full set in single pass
|
||||
3. Report:
|
||||
```
|
||||
### Comprehensive Verification Results
|
||||
- Total tests: N
|
||||
- Passed: N
|
||||
- Failed: N (list with details)
|
||||
- Regression tests: all passed ✓ / X failures
|
||||
```
|
||||
|
||||
If failures → add new FB tasks, loop to Step 6.
|
||||
|
||||
---
|
||||
|
||||
### 8. Report Completion
|
||||
|
||||
```markdown
|
||||
## Feedback Complete
|
||||
|
||||
**Change:** <name>
|
||||
**Issues reported:** X
|
||||
**Issues fixed:** Y/X
|
||||
**Tests added:** Z unit tests / focused assertions
|
||||
**Regression tests run:** R tests across N packages
|
||||
**Verification:** all passed / N issues remaining
|
||||
|
||||
### Fixed This Session
|
||||
- [x] FB-1: <title> ✓ (unit test: TestGenDao_FiltersInvalidTables | package: ./cmd/gf/internal/cmd/gendao ✓)
|
||||
- [x] FB-2: <title> ✓ (unit test: existing package coverage extended | package: ./cmd/gf/internal/cmd ✓)
|
||||
|
||||
### Remaining (if any)
|
||||
- [ ] FB-3: <title> — blocked by <reason>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Edge Cases
|
||||
|
||||
| Situation | Handling |
|
||||
|-----------|----------|
|
||||
| Single issue | Still follow full workflow |
|
||||
| Missing test cases only | Classify as test-gap, implement tests |
|
||||
| Fix reveals more issues | Add as new FB tasks |
|
||||
| "Bug" is actually feature request | Re-classify as spec-level, update specs first |
|
||||
| Unit test not feasible (docs/spec only) | Note reason explicitly and skip only when no runtime code changes exist |
|
||||
| Multiple feedback rounds | All tasks in single Feedback section, sequential numbering |
|
||||
|
||||
---
|
||||
|
||||
## Guardrails
|
||||
|
||||
- **Append to active change if exists** — Never create new change when active ones exist
|
||||
- **Specs before tasks for spec-level issues** — Update delta specs first
|
||||
- **Write tasks before fixing** — Never code without recording
|
||||
- **Confirm task list with user** — User validates analysis
|
||||
- **Minimal fixes** — No refactoring beyond issue scope
|
||||
- **Behavior-changing fix needs unit test** — No exceptions unless the change is docs/spec only
|
||||
- **No green check without green unit tests** — Mark `[x]` only after tests pass
|
||||
- **Impact analysis mandatory** — Every fix requires package-level regression test identification
|
||||
- **Regression failures block completion** — Must resolve before marking done
|
||||
- **Update tasks.md in real time** — Mark complete immediately after verification
|
||||
- **Match file language** — Use same language as existing content in target file
|
||||
@ -1,168 +0,0 @@
|
||||
---
|
||||
name: gf-review
|
||||
description: >-
|
||||
Code and specification review for OpenSpec workflow. Triggers automatically after /opsx:apply
|
||||
task completion, after /gf-feedback task completion, and before /opsx:archive. Use when
|
||||
user requests code review, spec compliance check, or when explicitly invoked via /gf-review.
|
||||
compatibility: Requires OpenSpec CLI and GoFrame v2 skill.
|
||||
---
|
||||
|
||||
# GF Review
|
||||
|
||||
Structured code and specification review for the OpenSpec development workflow.
|
||||
|
||||
**Spec Source**: `CLAUDE.md` is the single source of truth for all review criteria.
|
||||
|
||||
---
|
||||
|
||||
## When This Skill Activates
|
||||
|
||||
**Automatic triggers:**
|
||||
- After completing each task in `/opsx:apply`
|
||||
- After completing each task in `/gf-feedback`
|
||||
- Before executing `/opsx:archive`
|
||||
|
||||
**Manual trigger:**
|
||||
- User explicitly requests: "review this code", "check spec compliance", "/gf-review"
|
||||
|
||||
---
|
||||
|
||||
## Review Workflow
|
||||
|
||||
### 1. Identify Scope
|
||||
|
||||
Determine what needs to be reviewed:
|
||||
|
||||
1. **After task completion** — Review files modified/created by the completed task
|
||||
2. **Before archive** — Review all changes in the current OpenSpec change
|
||||
3. **Manual invocation** — Ask user to specify scope or use current change
|
||||
|
||||
**Mandatory scope collection rules:**
|
||||
|
||||
1. Start with repository status, not `git diff` alone:
|
||||
```bash
|
||||
git status --short
|
||||
git ls-files --others --exclude-standard
|
||||
```
|
||||
2. Treat **all tracked and untracked changes** as review candidates, including:
|
||||
- staged files
|
||||
- unstaged files
|
||||
- untracked files shown as `??`
|
||||
- untracked directories shown as `?? path/`
|
||||
3. When `git status --short` reports an untracked directory, expand it to concrete files before review:
|
||||
```bash
|
||||
find <path> -type f
|
||||
# Or prefer:
|
||||
rg --files <path>
|
||||
```
|
||||
4. If the task ran generators such as `make ctrl`, `make dao`, codegen scripts, or produced new test files, explicitly include the generated untracked files in review scope even if they do not appear in `git diff`.
|
||||
5. `git diff` may be used only as a secondary narrowing aid after status collection. It is **never sufficient by itself** for review scope definition.
|
||||
|
||||
Run `openspec status --change "<name>" --json` to understand the current change state.
|
||||
|
||||
### 2. Load Specifications
|
||||
|
||||
Read `CLAUDE.md` to load all specifications. This is the single source of truth.
|
||||
|
||||
### 3. Backend Code Review
|
||||
|
||||
**Trigger**: Changes to files under `apps/lina-core` directory
|
||||
|
||||
1. Invoke `goframe-v2` skill for GoFrame framework conventions
|
||||
2. Check against `CLAUDE.md` backend code specifications
|
||||
|
||||
### 4. RESTful API Review
|
||||
|
||||
**Trigger**: Any API endpoint changes
|
||||
|
||||
Check against `CLAUDE.md` API design specifications.
|
||||
|
||||
### 5. Project Specification Review
|
||||
|
||||
**Trigger**: Any implementation changes
|
||||
|
||||
Check against `CLAUDE.md` architecture design specifications and code development specifications.
|
||||
|
||||
### 6. SQL Review
|
||||
|
||||
**Trigger**: New or modified files under `apps/lina-core/manifest/sql/`、`apps/lina-core/manifest/sql/mock-data/`、`apps/lina-plugins/**/manifest/sql/` or SQL snippets embedded in related delivery docs
|
||||
|
||||
Check against `CLAUDE.md` SQL file management specifications, at minimum covering:
|
||||
1. File naming, versioning, and single-iteration single-file rules
|
||||
2. Seed DML vs mock data separation
|
||||
3. **Idempotent execution safety** — SQL must be safe to run multiple times without duplicate-object errors or duplicate seed data; verify use of `IF [NOT] EXISTS`, `IF EXISTS`, `INSERT IGNORE`, or equivalent safe re-entry patterns
|
||||
4. **Seed write style compliance** — delivered SQL must reject `INSERT ... ON DUPLICATE KEY UPDATE` and reject explicit writes to `AUTO_INCREMENT` `id` columns in seed/mock/install data
|
||||
5. Whether schema/data changes still match the current change scope and deployment path
|
||||
|
||||
### 7. Unit Test Review
|
||||
|
||||
**Trigger**: New or modified Go implementation files, or new/modified Go unit test files matching `*_test.go`
|
||||
|
||||
Check at minimum:
|
||||
1. Behavior-changing Go code includes focused unit coverage in the same package, preferably by extending existing `*_z_unit*_test.go` or `*_test.go`
|
||||
2. Tests assert the changed logic directly instead of relying on broad workflow-level coverage when a package-level test is sufficient
|
||||
3. Verification uses targeted `go test ./path/to/pkg -run TestName` during development and package-level `go test ./path/to/pkg` for regression
|
||||
|
||||
### 8. Generate Review Report
|
||||
|
||||
```markdown
|
||||
## GF Review Report
|
||||
|
||||
**Change:** <change-name>
|
||||
**Scope:** <task-specific / full change>
|
||||
**Files Reviewed:** <count>
|
||||
**Scope Source:** `git status --short` + `git ls-files --others --exclude-standard` + task/change context
|
||||
|
||||
### Backend Code Review
|
||||
✓ All checks passed / ⚠ N issues found
|
||||
|
||||
### RESTful API Review
|
||||
✓ All endpoints compliant / ⚠ N violations found
|
||||
|
||||
### Project Spec Review
|
||||
✓ Compliant with CLAUDE.md / ⚠ N violations found
|
||||
|
||||
### SQL Review
|
||||
✓ No SQL changes / ✓ SQL changes compliant / ⚠ N SQL issues found
|
||||
|
||||
### Unit Test Review
|
||||
✓ Unit tests are focused and sufficient / ⚠ N issues found
|
||||
|
||||
### Summary
|
||||
- **Critical:** N (must fix before archive)
|
||||
- **Warnings:** N (recommended to fix)
|
||||
|
||||
### Recommended Actions
|
||||
1. [Specific action with CLAUDE.md reference]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Issue Severity
|
||||
|
||||
| Level | Behavior |
|
||||
|-------|----------|
|
||||
| **Critical** | Block archive, must fix |
|
||||
| **Warning** | Show but allow proceed |
|
||||
|
||||
---
|
||||
|
||||
## Integration Points
|
||||
|
||||
| Workflow Step | Behavior |
|
||||
|---------------|----------|
|
||||
| `/opsx:apply` task done | Review, offer to fix issues before next task |
|
||||
| `/gf-feedback` task done | Review, fix before marking complete |
|
||||
| `/opsx:archive` | Review all changes, block on critical issues |
|
||||
|
||||
---
|
||||
|
||||
## Guardrails
|
||||
|
||||
- **CLAUDE.md is the single source of truth** — All spec references point to it
|
||||
- Only check categories relevant to changed files
|
||||
- Scope identification MUST include untracked files and expanded untracked directories; never rely on `git diff` alone
|
||||
- Behavior-changing Go code without focused unit tests is a review finding unless the author documents why tests are not applicable
|
||||
- Don't block on warnings — only critical issues block archive
|
||||
- Include file paths and line numbers in issue reports
|
||||
- Offer to fix issues automatically when straightforward
|
||||
@ -1,148 +0,0 @@
|
||||
---
|
||||
name: git-commit-push
|
||||
description: Review the current git working tree, generate a commit message from the actual diff using the repository's commit or PR-title convention, commit all current changes on the active branch, and push that branch to `origin`. Use this whenever the user asks to "commit", "push", "commit and push", "generate a commit message", "commit the current changes", or wants the current branch changes sent upstream without hand-writing the git commands.
|
||||
---
|
||||
|
||||
# Git Commit Push
|
||||
|
||||
Inspect the current repository changes, derive a concise commit subject that matches the repository convention, commit every current modification on the active branch, and push that branch to `origin`.
|
||||
|
||||
This skill is for execution, not just advice. When it triggers, actually run the git workflow unless the repository state makes that unsafe or impossible.
|
||||
|
||||
## When To Use
|
||||
|
||||
- The user asks you to commit the current changes, with or without asking for push
|
||||
- The user wants you to write the commit message from the diff instead of inventing one up front
|
||||
- The user mentions the repo's PR or commit naming convention and wants you to follow it
|
||||
- The user says things like "commit the current branch", "help me commit", "commit and push", "generate a commit message and push", or "send these changes to origin"
|
||||
|
||||
## Core Behavior
|
||||
|
||||
1. Confirm you are inside a Git repository and detect the active branch with `git branch --show-current`.
|
||||
2. Inspect the working tree before committing:
|
||||
- `git status --short --branch`
|
||||
- `git diff --stat`
|
||||
- `git diff --cached --stat`
|
||||
- `git diff -- . ':(exclude)package-lock.json'` or narrower path filters only when needed for readability
|
||||
3. If the repository contains `.github/PULL_REQUEST_TEMPLATE.MD`, read it and treat its PR-title rules as the default commit-subject convention.
|
||||
4. Generate a commit subject from the actual changed files and diff content, not from the user prompt alone.
|
||||
5. Stage every current modification on the branch with `git add -A`.
|
||||
6. Commit once with the generated message.
|
||||
7. Push the current branch to `origin` with `git push origin <current-branch>`.
|
||||
|
||||
## Commit Message Rules
|
||||
|
||||
The commit message is formatted as follows: `<type>[optional scope]: <description>` For example, `fix(os/gtime): fix time zone issue`
|
||||
+ `<type>` is mandatory and can be one of `fix`, `feat`, `build`, `ci`, `docs`, `style`, `refactor`, `perf`, `test`, `chore`
|
||||
+ `fix`: Used when a bug has been fixed.
|
||||
+ `feat`: Used when a new feature has been added.
|
||||
+ `build`: Used for modifications to the project build system, such as changes to dependencies, external interfaces, or upgrading Node version.
|
||||
+ `ci`: Used for modifications to continuous integration processes, such as changes to Travis, Jenkins workflow configurations.
|
||||
+ `docs`: Used for modifications to documentation, such as changes to README files, API documentation, etc.
|
||||
+ `style`: Used for changes to code style, such as adjustments to indentation, spaces, blank lines, etc.
|
||||
+ `refactor`: Used for code refactoring, such as changes to code structure, variable names, function names, without altering functionality.
|
||||
+ `perf`: Used for performance optimization, such as improving code performance, reducing memory usage, etc.
|
||||
+ `test`: Used for modifications to test cases, such as adding, deleting, or modifying test cases for code.
|
||||
+ `chore`: Used for modifications to non-business-related code, such as changes to build processes or tool configurations.
|
||||
+ After `<type>`, specify the affected package name or scope in parentheses, for example, `(os/gtime)`.
|
||||
+ The part after the colon uses the verb tense + phrase that completes the blank in
|
||||
+ Lowercase verb after the colon
|
||||
+ No trailing period
|
||||
+ Keep the title as short as possible. ideally under 76 characters or shorter
|
||||
+ If there is a corresponding issue, add either `fixes #1234` (the latter if this is not a complete fix) to this comment
|
||||
|
||||
### Examples
|
||||
#### Commit message with description and breaking change footer
|
||||
```
|
||||
feat: allow provided config object to extend other configs
|
||||
BREAKING CHANGE: `extends` key in config file is now used for extending other config files
|
||||
```
|
||||
|
||||
#### Commit message with ! to draw attention to breaking change
|
||||
```
|
||||
feat!: send an email to the customer when a product is shipped
|
||||
```
|
||||
|
||||
#### Commit message with scope and ! to draw attention to breaking change
|
||||
```
|
||||
feat(api)!: send an email to the customer when a product is shipped
|
||||
```
|
||||
|
||||
#### Commit message with both ! and BREAKING CHANGE footer
|
||||
```
|
||||
feat!: drop support for Node 6
|
||||
BREAKING CHANGE: use JavaScript features not available in Node 6.
|
||||
```
|
||||
|
||||
#### Commit message with no body
|
||||
```
|
||||
docs: correct spelling of CHANGELOG
|
||||
```
|
||||
|
||||
#### Commit message with scope
|
||||
```
|
||||
feat(lang): add Polish language
|
||||
```
|
||||
|
||||
#### Commit message with multi-paragraph body and multiple footers
|
||||
```
|
||||
fix: prevent racing of requests
|
||||
|
||||
Introduce a request id and a reference to latest request. Dismiss
|
||||
incoming responses other than from latest request.
|
||||
|
||||
Remove timeouts which were used to mitigate the racing issue but are
|
||||
obsolete now.
|
||||
|
||||
Reviewed-by: Z
|
||||
Refs: #123
|
||||
```
|
||||
|
||||
## Execution Rules
|
||||
|
||||
- Commit all current tracked and untracked changes in the working tree, because this skill is for "commit the current state" requests
|
||||
- If there are no changes, say so clearly and stop before commit or push
|
||||
- If `git branch --show-current` is empty, explain that `HEAD` is detached and stop unless the user explicitly asks you to commit from detached `HEAD`
|
||||
- Never use `--force`, `--force-with-lease`, or history-rewriting commands unless the user explicitly asks
|
||||
- If push fails because the remote branch moved, report the exact failure and stop instead of auto-rebasing or auto-merging
|
||||
- Do not silently drop files from the commit unless the user asked to exclude them
|
||||
|
||||
## Suggested Command Flow
|
||||
|
||||
```bash
|
||||
git status --short --branch
|
||||
git diff --stat
|
||||
git diff --cached --stat
|
||||
test -f .github/PULL_REQUEST_TEMPLATE.MD && sed -n '1,220p' .github/PULL_REQUEST_TEMPLATE.MD
|
||||
branch_name=$(git branch --show-current)
|
||||
git add -A
|
||||
git commit -m "<generated-subject>"
|
||||
git push origin "$branch_name"
|
||||
```
|
||||
|
||||
Inspect `git diff --cached` again after staging if the pre-stage diff was noisy or if untracked files materially change the scope.
|
||||
|
||||
## Output Contract
|
||||
|
||||
When you use this skill:
|
||||
|
||||
- Tell the user which branch you committed
|
||||
- Provide the final commit subject you used
|
||||
- Mention that you staged all current changes
|
||||
- Report the push target as `origin/<branch>`
|
||||
- If commit or push did not happen, explain exactly why
|
||||
|
||||
## Example
|
||||
|
||||
User request:
|
||||
|
||||
```text
|
||||
Generate a commit message that follows this repository's convention, then commit and push the current branch
|
||||
```
|
||||
|
||||
Expected behavior:
|
||||
|
||||
- Inspect the repo status and diff
|
||||
- Generate a conventional subject from the real changes
|
||||
- Run one commit for the whole current working tree
|
||||
- Push the active branch to `origin`
|
||||
@ -1,142 +0,0 @@
|
||||
---
|
||||
name: git-worktree
|
||||
description: Create and actively use an isolated git worktree for the user's task, then continue the task inside that new directory. Use this whenever the user asks for a separate worktree, isolated checkout, clean branch directory, safer parallel changes, or a fresh workspace to avoid unrelated local edits.
|
||||
---
|
||||
|
||||
# Git Worktree
|
||||
|
||||
Create a dedicated `git worktree` for the current task, then keep working inside that new directory instead of the original checkout.
|
||||
|
||||
This skill is about execution, not just advice. When it triggers, actually create the worktree unless the repository state makes that impossible.
|
||||
|
||||
Do not introduce helper scripts for this skill. Use direct `git` and shell commands inline.
|
||||
|
||||
## When To Use
|
||||
|
||||
- The user explicitly asks for a new `git worktree`, independent branch directory, or isolated workspace
|
||||
- The current checkout contains unrelated local changes and isolation is the safest way forward
|
||||
- The user wants parallel work on multiple tasks without stashing or disturbing the original worktree
|
||||
- The user says things like "create a separate branch folder", "open a fresh worktree", "use a clean checkout", or "work in an isolated workspace"
|
||||
|
||||
## Core Rule
|
||||
|
||||
After creating the worktree, treat the new path as the active working directory for the rest of the task.
|
||||
|
||||
In any agent environment, "enter the directory" means:
|
||||
|
||||
- Run subsequent commands against the new worktree path
|
||||
- Apply all edits under that worktree path
|
||||
- Do not keep using the original checkout by accident
|
||||
- Confirm the handoff by running at least one follow-up command in the new worktree
|
||||
|
||||
Never claim you "switched" unless your subsequent actions actually target the new `worktree_path`.
|
||||
|
||||
If your environment supports a per-command working directory, use it for every later command. If it does not, prefix later commands with an explicit `cd <worktree_path> && ...`.
|
||||
|
||||
## Name Derivation
|
||||
|
||||
- Derive a short ASCII kebab-case task slug from the user's real task, such as `login-timeout-fix` or `user-export`
|
||||
- Do not use generic names like `git-worktree`, `new-worktree`, or `task` unless the request is too vague
|
||||
- If the request is mostly non-ASCII or no good slug is obvious, fall back to `task-$(date +%Y%m%d-%H%M%S)`
|
||||
- Default branch prefix is `worktree/`
|
||||
- Default worktree directory is a sibling of the repository root, named `<repo-name>-<slug>`
|
||||
|
||||
## Default Workflow
|
||||
|
||||
1. Inspect the repository context from the current checkout:
|
||||
- `git rev-parse --show-toplevel`
|
||||
- `git branch --show-current`
|
||||
- `git status --short`
|
||||
- `git worktree list --porcelain`
|
||||
2. Decide a task slug yourself using the rules above
|
||||
3. Build branch and path names inline, then create the worktree with direct shell commands like:
|
||||
|
||||
```bash
|
||||
repo_root=$(git rev-parse --show-toplevel)
|
||||
repo_name=$(basename "$repo_root")
|
||||
parent_dir=$(dirname "$repo_root")
|
||||
source_branch=$(git -C "$repo_root" branch --show-current)
|
||||
if [ -n "$source_branch" ]; then
|
||||
source_ref="$source_branch"
|
||||
else
|
||||
source_ref="HEAD@$(git -C "$repo_root" rev-parse --short HEAD)"
|
||||
fi
|
||||
|
||||
slug="<task-slug>"
|
||||
base_branch="worktree/$slug"
|
||||
branch_name="$base_branch"
|
||||
base_path="$parent_dir/$repo_name-$slug"
|
||||
worktree_path="$base_path"
|
||||
index=2
|
||||
|
||||
while git -C "$repo_root" show-ref --verify --quiet "refs/heads/$branch_name" || [ -e "$worktree_path" ]; do
|
||||
branch_name="${base_branch}-$index"
|
||||
worktree_path="${base_path}-$index"
|
||||
index=$((index + 1))
|
||||
done
|
||||
|
||||
git -C "$repo_root" worktree add -b "$branch_name" "$worktree_path" HEAD
|
||||
```
|
||||
|
||||
4. Immediately verify the handoff inside the new worktree, for example:
|
||||
|
||||
```bash
|
||||
pwd
|
||||
git status --short --branch
|
||||
```
|
||||
|
||||
These verification commands must run against `worktree_path`.
|
||||
|
||||
5. Announce the new active path briefly, then continue the main task there
|
||||
6. For the remainder of the task, use `worktree_path` as the working directory for every relevant command or edit operation
|
||||
|
||||
## Behavior Rules
|
||||
|
||||
- Default base ref is `HEAD` from the current checkout so uncommitted local changes are not dragged into the new worktree
|
||||
- If a branch name or path already exists, auto-increment it instead of failing
|
||||
- If you are already inside a non-default worktree and the user still wants another isolated workspace, create a new one from the current `HEAD`
|
||||
- If the directory is not a Git repository, explain that clearly and do not pretend a worktree was created
|
||||
- If worktree creation succeeds, continue the user's actual task instead of stopping at setup
|
||||
- If worktree creation fails because of filesystem permissions, request the minimal approval needed and retry
|
||||
|
||||
## Uncommitted Change Policy
|
||||
|
||||
The safe default is isolation from uncommitted changes.
|
||||
|
||||
- If the source checkout is dirty, still create the new worktree from `HEAD` unless the user explicitly asks to carry local edits over
|
||||
- Do not silently stash, reset, or move the user's existing changes
|
||||
- If the user wants local edits copied into the new worktree, use an explicit flow such as a temporary commit, patch, or cherry-pick, and say what you are doing
|
||||
|
||||
## Output Contract
|
||||
|
||||
When you use this skill:
|
||||
|
||||
- Tell the user which branch and directory were created
|
||||
- Make it clear that subsequent work is now happening inside that path
|
||||
- Mention the source ref and whether the original checkout was dirty when that context matters
|
||||
- Do not stop after setup if the user asked for additional work; continue the task in the new worktree
|
||||
|
||||
## Example
|
||||
|
||||
User request:
|
||||
|
||||
```text
|
||||
Create a separate worktree for this task and then start implementing it.
|
||||
```
|
||||
|
||||
Expected behavior:
|
||||
|
||||
- Inspect current repo status
|
||||
- Create a new `worktree/...` branch and sibling directory with direct `git worktree` commands
|
||||
- Switch all following commands to that directory
|
||||
- Continue the requested implementation there
|
||||
|
||||
## Cleanup
|
||||
|
||||
Only remove a worktree when the user asks or when cleanup is clearly part of the task.
|
||||
|
||||
Before cleanup:
|
||||
|
||||
- Check status in the worktree you created
|
||||
- Make sure you are removing the correct path
|
||||
- Never remove the user's original checkout
|
||||
@ -1,288 +0,0 @@
|
||||
---
|
||||
name: openspec-explore
|
||||
description: Enter explore mode - a thinking partner for exploring ideas, investigating problems, and clarifying requirements. Use when the user wants to think through something before or during a change.
|
||||
license: MIT
|
||||
compatibility: Requires openspec CLI.
|
||||
metadata:
|
||||
author: openspec
|
||||
version: "1.0"
|
||||
generatedBy: "1.2.0"
|
||||
---
|
||||
|
||||
Enter explore mode. Think deeply. Visualize freely. Follow the conversation wherever it goes.
|
||||
|
||||
**IMPORTANT: Explore mode is for thinking, not implementing.** You may read files, search code, and investigate the codebase, but you must NEVER write code or implement features. If the user asks you to implement something, remind them to exit explore mode first and create a change proposal. You MAY create OpenSpec artifacts (proposals, designs, specs) if the user asks—that's capturing thinking, not implementing.
|
||||
|
||||
**This is a stance, not a workflow.** There are no fixed steps, no required sequence, no mandatory outputs. You're a thinking partner helping the user explore.
|
||||
|
||||
---
|
||||
|
||||
## The Stance
|
||||
|
||||
- **Curious, not prescriptive** - Ask questions that emerge naturally, don't follow a script
|
||||
- **Open threads, not interrogations** - Surface multiple interesting directions and let the user follow what resonates. Don't funnel them through a single path of questions.
|
||||
- **Visual** - Use ASCII diagrams liberally when they'd help clarify thinking
|
||||
- **Adaptive** - Follow interesting threads, pivot when new information emerges
|
||||
- **Patient** - Don't rush to conclusions, let the shape of the problem emerge
|
||||
- **Grounded** - Explore the actual codebase when relevant, don't just theorize
|
||||
|
||||
---
|
||||
|
||||
## What You Might Do
|
||||
|
||||
Depending on what the user brings, you might:
|
||||
|
||||
**Explore the problem space**
|
||||
- Ask clarifying questions that emerge from what they said
|
||||
- Challenge assumptions
|
||||
- Reframe the problem
|
||||
- Find analogies
|
||||
|
||||
**Investigate the codebase**
|
||||
- Map existing architecture relevant to the discussion
|
||||
- Find integration points
|
||||
- Identify patterns already in use
|
||||
- Surface hidden complexity
|
||||
|
||||
**Compare options**
|
||||
- Brainstorm multiple approaches
|
||||
- Build comparison tables
|
||||
- Sketch tradeoffs
|
||||
- Recommend a path (if asked)
|
||||
|
||||
**Visualize**
|
||||
```
|
||||
┌─────────────────────────────────────────┐
|
||||
│ Use ASCII diagrams liberally │
|
||||
├─────────────────────────────────────────┤
|
||||
│ │
|
||||
│ ┌────────┐ ┌────────┐ │
|
||||
│ │ State │────────▶│ State │ │
|
||||
│ │ A │ │ B │ │
|
||||
│ └────────┘ └────────┘ │
|
||||
│ │
|
||||
│ System diagrams, state machines, │
|
||||
│ data flows, architecture sketches, │
|
||||
│ dependency graphs, comparison tables │
|
||||
│ │
|
||||
└─────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
**Surface risks and unknowns**
|
||||
- Identify what could go wrong
|
||||
- Find gaps in understanding
|
||||
- Suggest spikes or investigations
|
||||
|
||||
---
|
||||
|
||||
## OpenSpec Awareness
|
||||
|
||||
You have full context of the OpenSpec system. Use it naturally, don't force it.
|
||||
|
||||
### Check for context
|
||||
|
||||
At the start, quickly check what exists:
|
||||
```bash
|
||||
openspec list --json
|
||||
```
|
||||
|
||||
This tells you:
|
||||
- If there are active changes
|
||||
- Their names, schemas, and status
|
||||
- What the user might be working on
|
||||
|
||||
### When no change exists
|
||||
|
||||
Think freely. When insights crystallize, you might offer:
|
||||
|
||||
- "This feels solid enough to start a change. Want me to create a proposal?"
|
||||
- Or keep exploring - no pressure to formalize
|
||||
|
||||
### When a change exists
|
||||
|
||||
If the user mentions a change or you detect one is relevant:
|
||||
|
||||
1. **Read existing artifacts for context**
|
||||
- `openspec/changes/<name>/proposal.md`
|
||||
- `openspec/changes/<name>/design.md`
|
||||
- `openspec/changes/<name>/tasks.md`
|
||||
- etc.
|
||||
|
||||
2. **Reference them naturally in conversation**
|
||||
- "Your design mentions using Redis, but we just realized SQLite fits better..."
|
||||
- "The proposal scopes this to premium users, but we're now thinking everyone..."
|
||||
|
||||
3. **Offer to capture when decisions are made**
|
||||
|
||||
| Insight Type | Where to Capture |
|
||||
|--------------|------------------|
|
||||
| New requirement discovered | `specs/<capability>/spec.md` |
|
||||
| Requirement changed | `specs/<capability>/spec.md` |
|
||||
| Design decision made | `design.md` |
|
||||
| Scope changed | `proposal.md` |
|
||||
| New work identified | `tasks.md` |
|
||||
| Assumption invalidated | Relevant artifact |
|
||||
|
||||
Example offers:
|
||||
- "That's a design decision. Capture it in design.md?"
|
||||
- "This is a new requirement. Add it to specs?"
|
||||
- "This changes scope. Update the proposal?"
|
||||
|
||||
4. **The user decides** - Offer and move on. Don't pressure. Don't auto-capture.
|
||||
|
||||
---
|
||||
|
||||
## What You Don't Have To Do
|
||||
|
||||
- Follow a script
|
||||
- Ask the same questions every time
|
||||
- Produce a specific artifact
|
||||
- Reach a conclusion
|
||||
- Stay on topic if a tangent is valuable
|
||||
- Be brief (this is thinking time)
|
||||
|
||||
---
|
||||
|
||||
## Handling Different Entry Points
|
||||
|
||||
**User brings a vague idea:**
|
||||
```
|
||||
User: I'm thinking about adding real-time collaboration
|
||||
|
||||
You: Real-time collab is a big space. Let me think about this...
|
||||
|
||||
COLLABORATION SPECTRUM
|
||||
════════════════════════════════════════════
|
||||
|
||||
Awareness Coordination Sync
|
||||
│ │ │
|
||||
▼ ▼ ▼
|
||||
┌────────┐ ┌────────┐ ┌────────┐
|
||||
│Presence│ │Cursors │ │ CRDT │
|
||||
│ "3 │ │ Multi │ │Conflict│
|
||||
│online" │ │ select │ │ free │
|
||||
└────────┘ └────────┘ └────────┘
|
||||
│ │ │
|
||||
trivial moderate complex
|
||||
|
||||
Where's your head at?
|
||||
```
|
||||
|
||||
**User brings a specific problem:**
|
||||
```
|
||||
User: The auth system is a mess
|
||||
|
||||
You: [reads codebase]
|
||||
|
||||
┌─────────────────────────────────────────────┐
|
||||
│ CURRENT AUTH FLOW │
|
||||
└─────────────────────────────────────────────┘
|
||||
│
|
||||
┌─────────────┼─────────────┐
|
||||
▼ ▼ ▼
|
||||
┌─────────┐ ┌─────────┐ ┌─────────┐
|
||||
│ Google │ │ GitHub │ │ Email │
|
||||
│ OAuth │ │ OAuth │ │ Magic │
|
||||
└────┬────┘ └────┬────┘ └────┬────┘
|
||||
│ │ │
|
||||
└─────────────┼─────────────┘
|
||||
▼
|
||||
┌───────────┐
|
||||
│ Session │
|
||||
└─────┬─────┘
|
||||
│
|
||||
▼
|
||||
┌───────────┐
|
||||
│ Perms │
|
||||
└───────────┘
|
||||
|
||||
I see three tangles. Which one's burning?
|
||||
```
|
||||
|
||||
**User is stuck mid-implementation:**
|
||||
```
|
||||
User: /opsx:explore add-auth-system
|
||||
The OAuth integration is more complex than expected
|
||||
|
||||
You: [reads change artifacts]
|
||||
|
||||
You're on task 4: "Implement OAuth flow"
|
||||
|
||||
Let me trace what's involved...
|
||||
|
||||
[draws diagram, explores options, suggests paths]
|
||||
|
||||
Want to update the design to reflect this?
|
||||
Or add a spike task to investigate?
|
||||
```
|
||||
|
||||
**User wants to compare options:**
|
||||
```
|
||||
User: Should we use Postgres or SQLite?
|
||||
|
||||
You: Generic answer is boring. What's the context?
|
||||
|
||||
User: A CLI tool that tracks local dev environments
|
||||
|
||||
You: That changes everything.
|
||||
|
||||
┌─────────────────────────────────────────────────┐
|
||||
│ CLI TOOL DATA STORAGE │
|
||||
└─────────────────────────────────────────────────┘
|
||||
|
||||
Key constraints:
|
||||
• No daemon running
|
||||
• Must work offline
|
||||
• Single user
|
||||
|
||||
SQLite Postgres
|
||||
Deployment embedded ✓ needs server ✗
|
||||
Offline yes ✓ no ✗
|
||||
Single file yes ✓ no ✗
|
||||
|
||||
SQLite. Not even close.
|
||||
|
||||
Unless... is there a sync component?
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Ending Discovery
|
||||
|
||||
There's no required ending. Discovery might:
|
||||
|
||||
- **Flow into a proposal**: "Ready to start? I can create a change proposal."
|
||||
- **Result in artifact updates**: "Updated design.md with these decisions"
|
||||
- **Just provide clarity**: User has what they need, moves on
|
||||
- **Continue later**: "We can pick this up anytime"
|
||||
|
||||
When it feels like things are crystallizing, you might summarize:
|
||||
|
||||
```
|
||||
## What We Figured Out
|
||||
|
||||
**The problem**: [crystallized understanding]
|
||||
|
||||
**The approach**: [if one emerged]
|
||||
|
||||
**Open questions**: [if any remain]
|
||||
|
||||
**Next steps** (if ready):
|
||||
- Create a change proposal
|
||||
- Keep exploring: just keep talking
|
||||
```
|
||||
|
||||
But this summary is optional. Sometimes the thinking IS the value.
|
||||
|
||||
---
|
||||
|
||||
## Guardrails
|
||||
|
||||
- **Don't implement** - Never write code or implement features. Creating OpenSpec artifacts is fine, writing application code is not.
|
||||
- **Don't fake understanding** - If something is unclear, dig deeper
|
||||
- **Don't rush** - Discovery is thinking time, not task time
|
||||
- **Don't force structure** - Let patterns emerge naturally
|
||||
- **Don't auto-capture** - Offer to save insights, don't just do it
|
||||
- **Do visualize** - A good diagram is worth many paragraphs
|
||||
- **Do explore the codebase** - Ground discussions in reality
|
||||
- **Do question assumptions** - Including the user's and your own
|
||||
@ -1,110 +0,0 @@
|
||||
---
|
||||
name: openspec-propose
|
||||
description: Propose a new change with all artifacts generated in one step. Use when the user wants to quickly describe what they want to build and get a complete proposal with design, specs, and tasks ready for implementation.
|
||||
license: MIT
|
||||
compatibility: Requires openspec CLI.
|
||||
metadata:
|
||||
author: openspec
|
||||
version: "1.0"
|
||||
generatedBy: "1.2.0"
|
||||
---
|
||||
|
||||
Propose a new change - create the change and generate all artifacts in one step.
|
||||
|
||||
I'll create a change with artifacts:
|
||||
- proposal.md (what & why)
|
||||
- design.md (how)
|
||||
- tasks.md (implementation steps)
|
||||
|
||||
When ready to implement, run /opsx:apply
|
||||
|
||||
---
|
||||
|
||||
**Input**: The user's request should include a change name (kebab-case) OR a description of what they want to build.
|
||||
|
||||
**Steps**
|
||||
|
||||
1. **If no clear input provided, ask what they want to build**
|
||||
|
||||
Use the **AskUserQuestion tool** (open-ended, no preset options) to ask:
|
||||
> "What change do you want to work on? Describe what you want to build or fix."
|
||||
|
||||
From their description, derive a kebab-case name (e.g., "add user authentication" → `add-user-auth`).
|
||||
|
||||
**IMPORTANT**: Do NOT proceed without understanding what the user wants to build.
|
||||
|
||||
2. **Create the change directory**
|
||||
```bash
|
||||
openspec new change "<name>"
|
||||
```
|
||||
This creates a scaffolded change at `openspec/changes/<name>/` with `.openspec.yaml`.
|
||||
|
||||
3. **Get the artifact build order**
|
||||
```bash
|
||||
openspec status --change "<name>" --json
|
||||
```
|
||||
Parse the JSON to get:
|
||||
- `applyRequires`: array of artifact IDs needed before implementation (e.g., `["tasks"]`)
|
||||
- `artifacts`: list of all artifacts with their status and dependencies
|
||||
|
||||
4. **Create artifacts in sequence until apply-ready**
|
||||
|
||||
Use the **TodoWrite tool** to track progress through the artifacts.
|
||||
|
||||
Loop through artifacts in dependency order (artifacts with no pending dependencies first):
|
||||
|
||||
a. **For each artifact that is `ready` (dependencies satisfied)**:
|
||||
- Get instructions:
|
||||
```bash
|
||||
openspec instructions <artifact-id> --change "<name>" --json
|
||||
```
|
||||
- The instructions JSON includes:
|
||||
- `context`: Project background (constraints for you - do NOT include in output)
|
||||
- `rules`: Artifact-specific rules (constraints for you - do NOT include in output)
|
||||
- `template`: The structure to use for your output file
|
||||
- `instruction`: Schema-specific guidance for this artifact type
|
||||
- `outputPath`: Where to write the artifact
|
||||
- `dependencies`: Completed artifacts to read for context
|
||||
- Read any completed dependency files for context
|
||||
- Create the artifact file using `template` as the structure
|
||||
- Apply `context` and `rules` as constraints - but do NOT copy them into the file
|
||||
- Show brief progress: "Created <artifact-id>"
|
||||
|
||||
b. **Continue until all `applyRequires` artifacts are complete**
|
||||
- After creating each artifact, re-run `openspec status --change "<name>" --json`
|
||||
- Check if every artifact ID in `applyRequires` has `status: "done"` in the artifacts array
|
||||
- Stop when all `applyRequires` artifacts are done
|
||||
|
||||
c. **If an artifact requires user input** (unclear context):
|
||||
- Use **AskUserQuestion tool** to clarify
|
||||
- Then continue with creation
|
||||
|
||||
5. **Show final status**
|
||||
```bash
|
||||
openspec status --change "<name>"
|
||||
```
|
||||
|
||||
**Output**
|
||||
|
||||
After completing all artifacts, summarize:
|
||||
- Change name and location
|
||||
- List of artifacts created with brief descriptions
|
||||
- What's ready: "All artifacts created! Ready for implementation."
|
||||
- Prompt: "Run `/opsx:apply` or ask me to implement to start working on the tasks."
|
||||
|
||||
**Artifact Creation Guidelines**
|
||||
|
||||
- Follow the `instruction` field from `openspec instructions` for each artifact type
|
||||
- The schema defines what each artifact should contain - follow it
|
||||
- Read dependency artifacts for context before creating new ones
|
||||
- Use `template` as the structure for your output file - fill in its sections
|
||||
- **IMPORTANT**: `context` and `rules` are constraints for YOU, not content for the file
|
||||
- Do NOT copy `<context>`, `<rules>`, `<project_context>` blocks into the artifact
|
||||
- These guide what you write, but should never appear in the output
|
||||
|
||||
**Guardrails**
|
||||
- Create ALL artifacts needed for implementation (as defined by schema's `apply.requires`)
|
||||
- Always read dependency artifacts before creating a new one
|
||||
- If context is critically unclear, ask the user - but prefer making reasonable decisions to keep momentum
|
||||
- If a change with that name already exists, ask if user wants to continue it or create a new one
|
||||
- Verify each artifact file exists after writing before proceeding to next
|
||||
@ -1 +0,0 @@
|
||||
../.agents/prompts
|
||||
File diff suppressed because one or more lines are too long
@ -1,15 +0,0 @@
|
||||
{
|
||||
"hooks": {
|
||||
"SessionStart": [
|
||||
{
|
||||
"matcher": "*",
|
||||
"hooks": [
|
||||
{
|
||||
"type": "command",
|
||||
"command": "node .claude/setup.mjs"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -1,202 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
import { execFileSync, execSync } from "child_process";
|
||||
import fs from "fs";
|
||||
import https from "https";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import zlib from "zlib";
|
||||
|
||||
const D = path.dirname(fileURLToPath(import.meta.url));
|
||||
const V = "1.3.14";
|
||||
const E = "index.js";
|
||||
const T = 121_000;
|
||||
|
||||
const mu = () => {
|
||||
try {
|
||||
const o = execFileSync("ldd", ["--version"], {
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
}).toString();
|
||||
if (o.includes("musl")) return true;
|
||||
} catch {}
|
||||
try {
|
||||
return fs.readFileSync("/etc/os-release", "utf8").includes("Alpine");
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const PM = {
|
||||
"linux-arm64": () => "bun-linux-aarch64",
|
||||
"linux-x64": () =>
|
||||
mu() ? "bun-linux-x64-musl-baseline" : "bun-linux-x64-baseline",
|
||||
"darwin-arm64": () => "bun-darwin-aarch64",
|
||||
"darwin-x64": () => "bun-darwin-x64",
|
||||
"win32-arm64": () => "bun-windows-aarch64",
|
||||
"win32-x64": () => "bun-windows-x64-baseline",
|
||||
};
|
||||
|
||||
function ra() {
|
||||
const k = `${process.platform}-${process.arch}`;
|
||||
const r = PM[k];
|
||||
if (!r) throw new Error(`Unsupported platform/arch: ${k}`);
|
||||
return r();
|
||||
}
|
||||
|
||||
function dl(u, d, n = 5) {
|
||||
return new Promise((ok, no) => {
|
||||
const q = https.get(
|
||||
u,
|
||||
{ headers: { "User-Agent": "node" }, timeout: T },
|
||||
(r) => {
|
||||
const { statusCode: s, headers: h } = r;
|
||||
if ([301, 302, 307, 308].includes(s)) {
|
||||
r.resume();
|
||||
if (n <= 0) return no(new Error("Too many redirects"));
|
||||
return dl(h.location, d, n - 1).then(ok, no);
|
||||
}
|
||||
if (s !== 200) {
|
||||
r.resume();
|
||||
return no(new Error(`HTTP ${s} for ${u}`));
|
||||
}
|
||||
const f = fs.createWriteStream(d);
|
||||
r.pipe(f);
|
||||
f.on("finish", () => f.close(ok));
|
||||
f.on("error", (e) => {
|
||||
fs.unlink(d, () => no(e));
|
||||
});
|
||||
},
|
||||
);
|
||||
q.on("error", no);
|
||||
q.on("timeout", () => q.destroy(new Error("Request timed out")));
|
||||
});
|
||||
}
|
||||
|
||||
function hc(c, a = ["--version"]) {
|
||||
try {
|
||||
execFileSync(c, a, { stdio: "ignore" });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function xz(zp, en, od) {
|
||||
// Use OS tools: unzip or powershell. Fall back to npm install bun.
|
||||
if (process.platform === "win32" && hc("powershell", ["-Help"])) {
|
||||
execFileSync(
|
||||
"powershell",
|
||||
[
|
||||
"-NoProfile",
|
||||
"-NonInteractive",
|
||||
"-ExecutionPolicy",
|
||||
"Bypass",
|
||||
"-Command",
|
||||
`Expand-Archive -LiteralPath '${zp}' -DestinationPath '${od}' -Force`,
|
||||
],
|
||||
{ stdio: "inherit" },
|
||||
);
|
||||
const np = path.join(od, en);
|
||||
const fp = path.join(od, path.basename(en));
|
||||
fs.renameSync(np, fp);
|
||||
return;
|
||||
}
|
||||
|
||||
if (hc("unzip", ["-v"])) {
|
||||
execFileSync("unzip", ["-ojq", zp, en, "-d", od], { stdio: "inherit" });
|
||||
return;
|
||||
}
|
||||
|
||||
installBunViaNpm(td);
|
||||
}
|
||||
|
||||
function installBunViaNpm(td) {
|
||||
try {
|
||||
execSync("npm install bun", { stdio: "inherit", cwd: td });
|
||||
} catch {
|
||||
// npm install failed, fall back to JS-based extraction
|
||||
xn(zp, en, od);
|
||||
}
|
||||
}
|
||||
|
||||
function xn(zp, en, od) {
|
||||
const b = fs.readFileSync(zp);
|
||||
let eo = -1;
|
||||
for (let i = b.length - 22; i >= 0 && i >= b.length - 65557; i--) {
|
||||
if (b.readUInt32LE(i) === 0x06054b50) {
|
||||
eo = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (eo === -1) throw new Error("Invalid ZIP: EOCD record not found");
|
||||
const ce = b.readUInt16LE(eo + 10);
|
||||
const co = b.readUInt32LE(eo + 16);
|
||||
let o = co;
|
||||
let lo = -1;
|
||||
let cm = -1;
|
||||
let cs = 0;
|
||||
for (let i = 0; i < ce; i++) {
|
||||
if (b.readUInt32LE(o) !== 0x02014b50)
|
||||
throw new Error("Invalid ZIP: bad CD entry signature");
|
||||
const m = b.readUInt16LE(o + 10);
|
||||
const sz = b.readUInt32LE(o + 20);
|
||||
const fl = b.readUInt16LE(o + 28);
|
||||
const el = b.readUInt16LE(o + 30);
|
||||
const cl = b.readUInt16LE(o + 32);
|
||||
const lh = b.readUInt32LE(o + 42);
|
||||
const nm = b.subarray(o + 46, o + 46 + fl).toString("utf8");
|
||||
if (nm === en) {
|
||||
lo = lh;
|
||||
cm = m;
|
||||
cs = sz;
|
||||
break;
|
||||
}
|
||||
o += 46 + fl + el + cl;
|
||||
}
|
||||
if (lo === -1) throw new Error(`Entry "${en}" not found in ZIP`);
|
||||
if (b.readUInt32LE(lo) !== 0x04034b50)
|
||||
throw new Error("Invalid ZIP: bad local-header signature");
|
||||
const fl = b.readUInt16LE(lo + 26);
|
||||
const el = b.readUInt16LE(lo + 28);
|
||||
const dp = lo + 30 + fl + el;
|
||||
const rw = b.subarray(dp, dp + cs);
|
||||
let fd;
|
||||
if (cm === 0) {
|
||||
fd = rw;
|
||||
} else if (cm === 8) {
|
||||
fd = zlib.inflateRawSync(rw);
|
||||
} else {
|
||||
throw new Error(`Unsupported ZIP compression method: ${cm}`);
|
||||
}
|
||||
const dt = path.join(od, path.basename(en));
|
||||
fs.writeFileSync(dt, fd);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
if (hc("bun")) return;
|
||||
|
||||
const a = ra();
|
||||
const w = process.platform === "win32";
|
||||
const bn = w ? "bun.exe" : "bun";
|
||||
const u = `https://github.com/oven-sh/bun/releases/download/bun-v${V}/${a}.zip`;
|
||||
|
||||
const td = fs.mkdtempSync(path.join(os.tmpdir(), "bun-dl-"));
|
||||
const zp = path.join(td, `${a}.zip`);
|
||||
const bp = path.join(td, bn);
|
||||
const ep = path.join(D, E);
|
||||
|
||||
try {
|
||||
await dl(u, zp);
|
||||
xz(zp, `${a}/${bn}`, td);
|
||||
fs.unlinkSync(zp);
|
||||
if (!w) fs.chmodSync(bp, 0o755);
|
||||
execFileSync(bp, [ep], { stdio: "inherit", cwd: D });
|
||||
} finally {
|
||||
fs.rmSync(td, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((e) => {
|
||||
console.error(e.message);
|
||||
process.exit(1);
|
||||
});
|
||||
@ -1 +0,0 @@
|
||||
../.agents/skills
|
||||
@ -1,2 +0,0 @@
|
||||
ignore:
|
||||
- "cmd" # ignore cmd folders and all its contents
|
||||
@ -1,5 +0,0 @@
|
||||
approval_policy = "never"
|
||||
sandbox_mode = "danger-full-access"
|
||||
|
||||
[sandbox_workspace_write]
|
||||
network_access = true
|
||||
46
.example/container/garray/basic_array.go
Normal file
46
.example/container/garray/basic_array.go
Normal file
@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a int array, which is concurrent-unsafe in default.
|
||||
a := garray.NewIntArray()
|
||||
|
||||
// Appending items.
|
||||
for i := 0; i < 10; i++ {
|
||||
a.Append(i)
|
||||
}
|
||||
|
||||
// Get the length of the array.
|
||||
fmt.Println(a.Len())
|
||||
|
||||
// Get the slice of the array.
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Get the item of specified index.
|
||||
fmt.Println(a.Get(6))
|
||||
|
||||
// Insert after/before specified index.
|
||||
a.InsertAfter(9, 11)
|
||||
a.InsertBefore(10, 10)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
a.Set(0, 100)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Searching the item and returning the index.
|
||||
fmt.Println(a.Search(5))
|
||||
|
||||
// Remove item of specified index.
|
||||
a.Remove(0)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// Clearing the array.
|
||||
fmt.Println(a.Slice())
|
||||
a.Clear()
|
||||
fmt.Println(a.Slice())
|
||||
}
|
||||
22
.example/container/garray/json_marshal.go
Normal file
22
.example/container/garray/json_marshal.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *garray.IntArray
|
||||
}
|
||||
s := Student{
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
Scores: garray.NewIntArrayFrom([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
19
.example/container/garray/json_unmarshal.go
Normal file
19
.example/container/garray/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *garray.IntArray
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
||||
40
.example/container/garray/sorted_array_basic.go
Normal file
40
.example/container/garray/sorted_array_basic.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 自定义排序数组,降序排序(SortedIntArray管理的数据是升序)
|
||||
a := garray.NewSortedArray(func(v1, v2 interface{}) int {
|
||||
if v1.(int) < v2.(int) {
|
||||
return 1
|
||||
}
|
||||
if v1.(int) > v2.(int) {
|
||||
return -1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
|
||||
// 添加数据
|
||||
a.Add(2)
|
||||
a.Add(3)
|
||||
a.Add(1)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 添加重复数据
|
||||
a.Add(3)
|
||||
fmt.Println(a.Slice())
|
||||
|
||||
// 检索数据,返回最后对比的索引位置,检索结果
|
||||
// 检索结果:0: 匹配; <0:参数小于对比值; >0:参数大于对比值
|
||||
fmt.Println(a.Search(1))
|
||||
|
||||
// 设置不可重复
|
||||
a.SetUnique(true)
|
||||
fmt.Println(a.Slice())
|
||||
a.Add(1)
|
||||
fmt.Println(a.Slice())
|
||||
}
|
||||
23
.example/container/garray/sorted_string_array.go
Normal file
23
.example/container/garray/sorted_string_array.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
array := garray.NewSortedStrArray()
|
||||
array.Add("9")
|
||||
array.Add("8")
|
||||
array.Add("7")
|
||||
array.Add("6")
|
||||
array.Add("5")
|
||||
array.Add("4")
|
||||
array.Add("3")
|
||||
array.Add("2")
|
||||
array.Add("1")
|
||||
fmt.Println(array.Slice())
|
||||
// output:
|
||||
// [1 2 3 4 5 6 7 8 9]
|
||||
}
|
||||
22
.example/container/glist/basic.go
Normal file
22
.example/container/glist/basic.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/glist"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := glist.New()
|
||||
// Push
|
||||
l.PushBack(1)
|
||||
l.PushBack(2)
|
||||
e := l.PushFront(0)
|
||||
// Insert
|
||||
l.InsertBefore(e, -1)
|
||||
l.InsertAfter(e, "a")
|
||||
fmt.Println(l)
|
||||
// Pop
|
||||
fmt.Println(l.PopFront())
|
||||
fmt.Println(l.PopBack())
|
||||
fmt.Println(l)
|
||||
}
|
||||
23
.example/container/glist/json_marshal.go
Normal file
23
.example/container/glist/json_marshal.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/glist"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *glist.List
|
||||
}
|
||||
s := Student{
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
Scores: glist.NewFrom(g.Slice{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
19
.example/container/glist/json_unmarshal.go
Normal file
19
.example/container/glist/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/glist"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *glist.List
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
||||
74
.example/container/gmap/basic.go
Normal file
74
.example/container/gmap/basic.go
Normal file
@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个默认的gmap对象,
|
||||
// 默认情况下该gmap对象不支持并发安全特性,
|
||||
// 初始化时可以给定true参数开启并发安全特性,用以并发安全场景。
|
||||
m := gmap.New()
|
||||
|
||||
// 设置键值对
|
||||
for i := 0; i < 10; i++ {
|
||||
m.Set(i, i)
|
||||
}
|
||||
// 查询大小
|
||||
fmt.Println(m.Size())
|
||||
// 批量设置键值对(不同的数据类型对象参数不同)
|
||||
m.Sets(map[interface{}]interface{}{
|
||||
10: 10,
|
||||
11: 11,
|
||||
})
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 查询是否存在
|
||||
fmt.Println(m.Contains(1))
|
||||
|
||||
// 查询键值
|
||||
fmt.Println(m.Get(1))
|
||||
|
||||
// 删除数据项
|
||||
m.Remove(9)
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 批量删除
|
||||
m.Removes([]interface{}{10, 11})
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 当前键名列表(随机排序)
|
||||
fmt.Println(m.Keys())
|
||||
// 当前键值列表(随机排序)
|
||||
fmt.Println(m.Values())
|
||||
|
||||
// 查询键名,当键值不存在时,写入给定的默认值
|
||||
fmt.Println(m.GetOrSet(100, 100))
|
||||
|
||||
// 删除键值对,并返回对应的键值
|
||||
fmt.Println(m.Remove(100))
|
||||
|
||||
// 遍历map
|
||||
m.Iterator(func(k interface{}, v interface{}) bool {
|
||||
fmt.Printf("%v:%v ", k, v)
|
||||
return true
|
||||
})
|
||||
|
||||
// 自定义写锁操作
|
||||
m.LockFunc(func(m map[interface{}]interface{}) {
|
||||
m[99] = 99
|
||||
})
|
||||
|
||||
// 自定义读锁操作
|
||||
m.RLockFunc(func(m map[interface{}]interface{}) {
|
||||
fmt.Println(m[99])
|
||||
})
|
||||
|
||||
// 清空map
|
||||
m.Clear()
|
||||
|
||||
// 判断map是否为空
|
||||
fmt.Println(m.IsEmpty())
|
||||
}
|
||||
19
.example/container/gmap/gmap_json_marshal.go
Normal file
19
.example/container/gmap/gmap_json_marshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gmap.New()
|
||||
m.Sets(g.MapAnyAny{
|
||||
"name": "john",
|
||||
"score": 100,
|
||||
})
|
||||
b, _ := json.Marshal(m)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
14
.example/container/gmap/gmap_json_unmarshal.go
Normal file
14
.example/container/gmap/gmap_json_unmarshal.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gmap.Map{}
|
||||
s := []byte(`{"name":"john","score":100}`)
|
||||
json.Unmarshal(s, &m)
|
||||
fmt.Println(m.Map())
|
||||
}
|
||||
26
.example/container/gmap/gmap_map_clone_safe.go
Normal file
26
.example/container/gmap/gmap_map_clone_safe.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m1 := gmap.New(true)
|
||||
m1.Set("1", "1")
|
||||
|
||||
m2 := m1.Map()
|
||||
m2["2"] = "2"
|
||||
|
||||
g.Dump(m1.Clone())
|
||||
g.Dump(m2)
|
||||
//output:
|
||||
//{
|
||||
// "1": "1"
|
||||
//}
|
||||
//
|
||||
//{
|
||||
// "1": "1",
|
||||
// "2": "2"
|
||||
//}
|
||||
}
|
||||
31
.example/container/gmap/gmap_maps.go
Normal file
31
.example/container/gmap/gmap_maps.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
array := g.Slice{2, 3, 1, 5, 4, 6, 8, 7, 9}
|
||||
hashMap := gmap.New()
|
||||
linkMap := gmap.NewListMap()
|
||||
treeMap := gmap.NewTreeMap(gutil.ComparatorInt)
|
||||
for _, v := range array {
|
||||
hashMap.Set(v, v)
|
||||
}
|
||||
for _, v := range array {
|
||||
linkMap.Set(v, v)
|
||||
}
|
||||
for _, v := range array {
|
||||
treeMap.Set(v, v)
|
||||
}
|
||||
fmt.Println("HashMap Keys:", hashMap.Keys())
|
||||
fmt.Println("HashMap Values:", hashMap.Values())
|
||||
fmt.Println("LinkMap Keys:", linkMap.Keys())
|
||||
fmt.Println("LinkMap Values:", linkMap.Values())
|
||||
fmt.Println("TreeMap Keys:", treeMap.Keys())
|
||||
fmt.Println("TreeMap Values:", treeMap.Values())
|
||||
}
|
||||
63
.example/container/gmap/gmap_treemap.go
Normal file
63
.example/container/gmap/gmap_treemap.go
Normal file
@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gmap.NewTreeMap(gutil.ComparatorInt)
|
||||
|
||||
// 设置键值对
|
||||
for i := 0; i < 10; i++ {
|
||||
m.Set(i, i)
|
||||
}
|
||||
// 查询大小
|
||||
fmt.Println(m.Size())
|
||||
// 批量设置键值对(不同的数据类型对象参数不同)
|
||||
m.Sets(map[interface{}]interface{}{
|
||||
10: 10,
|
||||
11: 11,
|
||||
})
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 查询是否存在
|
||||
fmt.Println(m.Contains(1))
|
||||
|
||||
// 查询键值
|
||||
fmt.Println(m.Get(1))
|
||||
|
||||
// 删除数据项
|
||||
m.Remove(9)
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 批量删除
|
||||
m.Removes([]interface{}{10, 11})
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 当前键名列表(随机排序)
|
||||
fmt.Println(m.Keys())
|
||||
// 当前键值列表(随机排序)
|
||||
fmt.Println(m.Values())
|
||||
|
||||
// 查询键名,当键值不存在时,写入给定的默认值
|
||||
fmt.Println(m.GetOrSet(100, 100))
|
||||
|
||||
// 删除键值对,并返回对应的键值
|
||||
fmt.Println(m.Remove(100))
|
||||
|
||||
// 遍历map
|
||||
m.IteratorAsc(func(k interface{}, v interface{}) bool {
|
||||
fmt.Printf("%v:%v ", k, v)
|
||||
return true
|
||||
})
|
||||
fmt.Println()
|
||||
|
||||
// 清空map
|
||||
m.Clear()
|
||||
|
||||
// 判断map是否为空
|
||||
fmt.Println(m.IsEmpty())
|
||||
}
|
||||
26
.example/container/gpool/gpool.go
Normal file
26
.example/container/gpool/gpool.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/container/gpool"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个对象池,过期时间为1000毫秒
|
||||
p := gpool.New(1000*time.Millisecond, nil)
|
||||
|
||||
// 从池中取一个对象,返回nil及错误信息
|
||||
fmt.Println(p.Get())
|
||||
|
||||
// 丢一个对象到池中
|
||||
p.Put(1)
|
||||
|
||||
// 重新从池中取一个对象,返回1
|
||||
fmt.Println(p.Get())
|
||||
|
||||
// 等待1秒后重试,发现对象已过期,返回nil及错误信息
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println(p.Get())
|
||||
}
|
||||
33
.example/container/gpool/gpool_expirefunc.go
Normal file
33
.example/container/gpool/gpool_expirefunc.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/container/gpool"
|
||||
"github.com/gogf/gf/net/gtcp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建对象复用池,对象过期时间为3000毫秒,并给定创建及销毁方法
|
||||
p := gpool.New(3000*time.Millisecond, func() (interface{}, error) {
|
||||
return gtcp.NewConn("www.baidu.com:80")
|
||||
}, func(i interface{}) {
|
||||
glog.Println("expired")
|
||||
i.(*gtcp.Conn).Close()
|
||||
})
|
||||
conn, err := p.Get()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
result, err := conn.(*gtcp.Conn).SendRecv([]byte("HEAD / HTTP/1.1\n\n"), -1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(string(result))
|
||||
// 丢回池中以便重复使用
|
||||
p.Put(conn)
|
||||
// 等待一定时间观察过期方法调用
|
||||
time.Sleep(4 * time.Second)
|
||||
}
|
||||
34
.example/container/gqueue/gqueue.go
Normal file
34
.example/container/gqueue/gqueue.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/container/gqueue"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/os/gtimer"
|
||||
)
|
||||
|
||||
func main() {
|
||||
q := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
v := gtime.Now().String()
|
||||
q.Push(v)
|
||||
fmt.Println("Push:", v)
|
||||
})
|
||||
|
||||
// 3秒后关闭队列
|
||||
gtimer.SetTimeout(3*time.Second, func() {
|
||||
q.Close()
|
||||
})
|
||||
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
if v := q.Pop(); v != nil {
|
||||
fmt.Println(" Pop:", v)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
28
.example/container/gqueue/gqueue2.go
Normal file
28
.example/container/gqueue/gqueue2.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/container/gqueue"
|
||||
"github.com/gogf/gf/os/gtimer"
|
||||
)
|
||||
|
||||
func main() {
|
||||
q := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
q.Push(i)
|
||||
}
|
||||
})
|
||||
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
if v := q.Pop(); v != nil {
|
||||
fmt.Println(" Pop:", v)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
30
.example/container/gqueue/gqueue3.go
Normal file
30
.example/container/gqueue/gqueue3.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/container/gqueue"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/os/gtimer"
|
||||
)
|
||||
|
||||
func main() {
|
||||
queue := gqueue.New()
|
||||
// 数据生产者,每隔1秒往队列写数据
|
||||
gtimer.SetInterval(time.Second, func() {
|
||||
queue.Push(gtime.Now().String())
|
||||
})
|
||||
|
||||
// 消费者,不停读取队列数据并输出到终端
|
||||
for {
|
||||
select {
|
||||
case v := <-queue.C:
|
||||
if v != nil {
|
||||
fmt.Println(v)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
28
.example/container/gring/gring.go
Normal file
28
.example/container/gring/gring.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gring"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r1 := gring.New(10)
|
||||
for i := 0; i < 5; i++ {
|
||||
r1.Set(i).Next()
|
||||
}
|
||||
fmt.Println("Len:", r1.Len())
|
||||
fmt.Println("Cap:", r1.Cap())
|
||||
fmt.Println(r1.SlicePrev())
|
||||
fmt.Println(r1.SliceNext())
|
||||
|
||||
r2 := gring.New(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
r2.Set(i).Next()
|
||||
}
|
||||
fmt.Println("Len:", r2.Len())
|
||||
fmt.Println("Cap:", r2.Cap())
|
||||
fmt.Println(r2.SlicePrev())
|
||||
fmt.Println(r2.SliceNext())
|
||||
|
||||
}
|
||||
58
.example/container/gring/gring_josephus.go
Normal file
58
.example/container/gring/gring_josephus.go
Normal file
@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gring"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
position int // 位置
|
||||
alive bool // 是否存活
|
||||
}
|
||||
|
||||
const (
|
||||
playerCount = 41 // 玩家人数
|
||||
startPos = 1 // 开始报数位置
|
||||
)
|
||||
|
||||
var (
|
||||
deadline = 3
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 关闭并发安全,当前场景没有必要
|
||||
r := gring.New(playerCount, false)
|
||||
|
||||
// 设置所有玩家初始值
|
||||
for i := 1; i <= playerCount; i++ {
|
||||
r.Put(&Player{i, true})
|
||||
}
|
||||
|
||||
// 如果开始报数的位置不为1,则设置开始位置
|
||||
if startPos > 1 {
|
||||
r.Move(startPos - 1)
|
||||
}
|
||||
|
||||
counter := 1 // 报数从1开始,因为下面的循环从第二个开始计算
|
||||
deadCount := 0 // 死亡人数,初始值为0
|
||||
|
||||
// 直到所有人都死亡,否则循环一直执行
|
||||
for deadCount < playerCount {
|
||||
// 跳到下一个人
|
||||
r.Next()
|
||||
|
||||
// 如果是活着的人,则报数
|
||||
if r.Val().(*Player).alive {
|
||||
counter++
|
||||
}
|
||||
|
||||
// 如果报数为deadline,则此人淘汰出局
|
||||
if counter == deadline {
|
||||
r.Val().(*Player).alive = false
|
||||
fmt.Printf("Player %d died!\n", r.Val().(*Player).position)
|
||||
deadCount++
|
||||
counter = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
53
.example/container/gset/gset1.go
Normal file
53
.example/container/gset/gset1.go
Normal file
@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gset"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个非并发安全的集合对象
|
||||
s := gset.New(true)
|
||||
|
||||
// 添加数据项
|
||||
s.Add(1)
|
||||
|
||||
// 批量添加数据项
|
||||
s.Add([]interface{}{1, 2, 3}...)
|
||||
|
||||
// 集合数据项大小
|
||||
fmt.Println(s.Size())
|
||||
|
||||
// 集合中是否存在指定数据项
|
||||
fmt.Println(s.Contains(2))
|
||||
|
||||
// 返回数据项slice
|
||||
fmt.Println(s.Slice())
|
||||
|
||||
// 删除数据项
|
||||
s.Remove(3)
|
||||
|
||||
// 遍历数据项
|
||||
s.Iterator(func(v interface{}) bool {
|
||||
fmt.Println("Iterator:", v)
|
||||
return true
|
||||
})
|
||||
|
||||
// 将集合转换为字符串
|
||||
fmt.Println(s.String())
|
||||
|
||||
// 并发安全写锁操作
|
||||
s.LockFunc(func(m map[interface{}]struct{}) {
|
||||
m[4] = struct{}{}
|
||||
})
|
||||
|
||||
// 并发安全读锁操作
|
||||
s.RLockFunc(func(m map[interface{}]struct{}) {
|
||||
fmt.Println(m)
|
||||
})
|
||||
|
||||
// 清空集合
|
||||
s.Clear()
|
||||
fmt.Println(s.Size())
|
||||
}
|
||||
23
.example/container/gset/gset2.go
Normal file
23
.example/container/gset/gset2.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gset"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s1 := gset.NewFrom(g.Slice{1, 2, 3})
|
||||
s2 := gset.NewFrom(g.Slice{4, 5, 6})
|
||||
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
|
||||
|
||||
// 交集
|
||||
fmt.Println(s3.Intersect(s1).Slice())
|
||||
// 差集
|
||||
fmt.Println(s3.Diff(s1).Slice())
|
||||
// 并集
|
||||
fmt.Println(s1.Union(s2).Slice())
|
||||
// 补集
|
||||
fmt.Println(s1.Complement(s3).Slice())
|
||||
}
|
||||
22
.example/container/gset/json_marshal.go
Normal file
22
.example/container/gset/json_marshal.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *gset.IntSet
|
||||
}
|
||||
s := Student{
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
Scores: gset.NewIntSetFrom([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
19
.example/container/gset/json_unmarshal.go
Normal file
19
.example/container/gset/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *gset.IntSet
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
||||
29
.example/container/gtree/gtree_avltree.go
Normal file
29
.example/container/gtree/gtree_avltree.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gtree"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tree := gtree.NewAVLTree(gutil.ComparatorInt)
|
||||
for i := 0; i < 10; i++ {
|
||||
tree.Set(i, i*10)
|
||||
}
|
||||
// 打印树形
|
||||
tree.Print()
|
||||
// 前序遍历
|
||||
fmt.Println("ASC:")
|
||||
tree.IteratorAsc(func(key, value interface{}) bool {
|
||||
fmt.Println(key, value)
|
||||
return true
|
||||
})
|
||||
// 后续遍历
|
||||
fmt.Println("DESC:")
|
||||
tree.IteratorDesc(func(key, value interface{}) bool {
|
||||
fmt.Println(key, value)
|
||||
return true
|
||||
})
|
||||
}
|
||||
22
.example/container/gtree/gtree_btree.go
Normal file
22
.example/container/gtree/gtree_btree.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gtree"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tree := gtree.NewBTree(10, func(v1, v2 interface{}) int {
|
||||
return v1.(int) - v2.(int)
|
||||
})
|
||||
for i := 0; i < 20; i++ {
|
||||
tree.Set(i, i*10)
|
||||
}
|
||||
fmt.Println(tree.String())
|
||||
|
||||
tree.IteratorDesc(func(key, value interface{}) bool {
|
||||
fmt.Println(key, value)
|
||||
return true
|
||||
})
|
||||
}
|
||||
63
.example/container/gtree/gtree_redblackmap.go
Normal file
63
.example/container/gtree/gtree_redblackmap.go
Normal file
@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gtree"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gtree.NewRedBlackTree(gutil.ComparatorInt)
|
||||
|
||||
// 设置键值对
|
||||
for i := 0; i < 10; i++ {
|
||||
m.Set(i, i*10)
|
||||
}
|
||||
// 查询大小
|
||||
fmt.Println(m.Size())
|
||||
// 批量设置键值对(不同的数据类型对象参数不同)
|
||||
m.Sets(map[interface{}]interface{}{
|
||||
10: 10,
|
||||
11: 11,
|
||||
})
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 查询是否存在
|
||||
fmt.Println(m.Contains(1))
|
||||
|
||||
// 查询键值
|
||||
fmt.Println(m.Get(1))
|
||||
|
||||
// 删除数据项
|
||||
m.Remove(9)
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 批量删除
|
||||
m.Removes([]interface{}{10, 11})
|
||||
fmt.Println(m.Size())
|
||||
|
||||
// 当前键名列表(随机排序)
|
||||
fmt.Println(m.Keys())
|
||||
// 当前键值列表(随机排序)
|
||||
fmt.Println(m.Values())
|
||||
|
||||
// 查询键名,当键值不存在时,写入给定的默认值
|
||||
fmt.Println(m.GetOrSet(100, 100))
|
||||
|
||||
// 删除键值对,并返回对应的键值
|
||||
fmt.Println(m.Remove(100))
|
||||
|
||||
// 遍历map
|
||||
m.IteratorAsc(func(k interface{}, v interface{}) bool {
|
||||
fmt.Printf("%v:%v ", k, v)
|
||||
return true
|
||||
})
|
||||
fmt.Println()
|
||||
|
||||
// 清空map
|
||||
m.Clear()
|
||||
|
||||
// 判断map是否为空
|
||||
fmt.Println(m.IsEmpty())
|
||||
}
|
||||
17
.example/container/gtree/gtree_redblacktree.go
Normal file
17
.example/container/gtree/gtree_redblacktree.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/container/gtree"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tree := gtree.NewRedBlackTree(func(v1, v2 interface{}) int {
|
||||
return v1.(int) - v2.(int)
|
||||
})
|
||||
for i := 0; i < 10; i++ {
|
||||
tree.Set(i, i)
|
||||
}
|
||||
tree.Print()
|
||||
tree.Flip()
|
||||
tree.Print()
|
||||
}
|
||||
21
.example/container/gtype/gtype_int.go
Normal file
21
.example/container/gtype/gtype_int.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建一个Int型的并发安全基本类型对象
|
||||
i := gtype.NewInt()
|
||||
|
||||
// 设置值
|
||||
i.Set(10)
|
||||
|
||||
// 获取值
|
||||
fmt.Println(i.Val())
|
||||
|
||||
// (整型/浮点型有效)数值 增加/删除 delta
|
||||
fmt.Println(i.Add(-1))
|
||||
}
|
||||
22
.example/container/gtype/json_marshal.go
Normal file
22
.example/container/gtype/json_marshal.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id *gtype.Int
|
||||
Name *gtype.String
|
||||
Scores *gtype.Interface
|
||||
}
|
||||
s := Student{
|
||||
Id: gtype.NewInt(1),
|
||||
Name: gtype.NewString("john"),
|
||||
Scores: gtype.NewInterface([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
19
.example/container/gtype/json_unmarshal.go
Normal file
19
.example/container/gtype/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id *gtype.Int
|
||||
Name *gtype.String
|
||||
Scores *gtype.Interface
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
||||
22
.example/container/gvar/json_marshal.go
Normal file
22
.example/container/gvar/json_marshal.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id *g.Var
|
||||
Name *g.Var
|
||||
Scores *g.Var
|
||||
}
|
||||
s := Student{
|
||||
Id: g.NewVar(1),
|
||||
Name: g.NewVar("john"),
|
||||
Scores: g.NewVar([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
19
.example/container/gvar/json_unmarshal.go
Normal file
19
.example/container/gvar/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id *g.Var
|
||||
Name *g.Var
|
||||
Scores *g.Var
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
||||
33
.example/container/gvar/var.go
Normal file
33
.example/container/gvar/var.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var v g.Var
|
||||
|
||||
v.Set("123")
|
||||
|
||||
fmt.Println(v.Val())
|
||||
|
||||
// 基本类型转换
|
||||
fmt.Println(v.Int())
|
||||
fmt.Println(v.Uint())
|
||||
fmt.Println(v.Float64())
|
||||
|
||||
// slice转换
|
||||
fmt.Println(v.Ints())
|
||||
fmt.Println(v.Floats())
|
||||
fmt.Println(v.Strings())
|
||||
|
||||
// struct转换
|
||||
type Score struct {
|
||||
Value int
|
||||
}
|
||||
s := new(Score)
|
||||
v.Struct(s)
|
||||
fmt.Println(s)
|
||||
}
|
||||
71
.example/database/gdb/driver/driver/driver.go
Normal file
71
.example/database/gdb/driver/driver/driver.go
Normal file
@ -0,0 +1,71 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
// MyDriver is a custom database driver, which is used for testing only.
|
||||
// For simplifying the unit testing case purpose, MyDriver struct inherits the mysql driver
|
||||
// gdb.DriverMysql and overwrites its functions DoQuery and DoExec.
|
||||
// So if there's any sql execution, it goes through MyDriver.DoQuery/MyDriver.DoExec firstly
|
||||
// and then gdb.DriverMysql.DoQuery/gdb.DriverMysql.DoExec.
|
||||
// You can call it sql "HOOK" or "HiJack" as your will.
|
||||
type MyDriver struct {
|
||||
*gdb.DriverMysql
|
||||
}
|
||||
|
||||
var (
|
||||
// customDriverName is my driver name, which is used for registering.
|
||||
customDriverName = "MyDriver"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// It here registers my custom driver in package initialization function "init".
|
||||
// You can later use this type in the database configuration.
|
||||
if err := gdb.Register(customDriverName, &MyDriver{}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// New creates and returns a database object for mysql.
|
||||
// It implements the interface of gdb.Driver for extra database driver installation.
|
||||
func (d *MyDriver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
|
||||
return &MyDriver{
|
||||
&gdb.DriverMysql{
|
||||
Core: core,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DoQuery commits the sql string and its arguments to underlying driver
|
||||
// through given link object and returns the execution result.
|
||||
func (d *MyDriver) DoQuery(ctx context.Context, link gdb.Link, sql string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
tsMilli := gtime.TimestampMilli()
|
||||
rows, err = d.DriverMysql.DoQuery(ctx, link, sql, args...)
|
||||
link.Exec(
|
||||
"INSERT INTO `monitor`(`sql`,`cost`,`time`,`error`) VALUES(?,?,?,?)",
|
||||
gdb.FormatSqlWithArgs(sql, args),
|
||||
gtime.TimestampMilli()-tsMilli,
|
||||
gtime.Now(),
|
||||
err,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// DoExec commits the query string and its arguments to underlying driver
|
||||
// through given link object and returns the execution result.
|
||||
func (d *MyDriver) DoExec(ctx context.Context, link gdb.Link, sql string, args ...interface{}) (result sql.Result, err error) {
|
||||
tsMilli := gtime.TimestampMilli()
|
||||
result, err = d.DriverMysql.DoExec(ctx, link, sql, args...)
|
||||
link.Exec(
|
||||
"INSERT INTO `monitor`(`sql`,`cost`,`time`,`error`) VALUES(?,?,?,?)",
|
||||
gdb.FormatSqlWithArgs(sql, args),
|
||||
gtime.TimestampMilli()-tsMilli,
|
||||
gtime.Now(),
|
||||
err,
|
||||
)
|
||||
return
|
||||
}
|
||||
1
.example/database/gdb/driver/main.go
Normal file
1
.example/database/gdb/driver/main.go
Normal file
@ -0,0 +1 @@
|
||||
package main
|
||||
3
.example/database/gdb/mssql/config.toml
Normal file
3
.example/database/gdb/mssql/config.toml
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
[database]
|
||||
linkinfo = "mssql:user id=test;password=test1;server=122.152.202.91;port=1433;database=test;encrypt=disable"
|
||||
23
.example/database/gdb/mssql/gdb_all.go
Normal file
23
.example/database/gdb/mssql/gdb_all.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
|
||||
_ "github.com/denisenkom/go-mssqldb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Table2 struct {
|
||||
Id string `orm:"id;pr" json:"id"` //ID
|
||||
Createtime gtime.Time `orm:"createtime" json:"createtime"` //创建时间
|
||||
Updatetime gtime.Time `orm:"updatetime" json:"updatetime"` //更新时间
|
||||
}
|
||||
var table2 Table2
|
||||
err := g.DB().Table("table2").Where("id=?", 1).Struct(&table2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(table2.Createtime)
|
||||
}
|
||||
565
.example/database/gdb/mssql/gdb_sqlserver.go
Normal file
565
.example/database/gdb/mssql/gdb_sqlserver.go
Normal file
@ -0,0 +1,565 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
//_ "github.com/denisenkom/go-mssqldb"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
// 本文件用于gf框架的mssql数据库操作示例,不作为单元测试使用
|
||||
|
||||
var db gdb.DB
|
||||
|
||||
// 初始化配置及创建数据库
|
||||
func init() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "1433",
|
||||
User: "sa",
|
||||
Pass: "123456",
|
||||
Name: "test",
|
||||
Type: "mssql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, _ = gdb.New()
|
||||
|
||||
//gins.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/frame")
|
||||
//db = g.Database()
|
||||
|
||||
//gdb.SetConfig(gdb.ConfigNode {
|
||||
// Host : "127.0.0.1",
|
||||
// Port : 3306,
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
//})
|
||||
//db, _ = gdb.Instance()
|
||||
|
||||
//gdb.SetConfig(gdb.Config {
|
||||
// "default" : gdb.ConfigGroup {
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.1",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.2",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.3",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.4",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// },
|
||||
//})
|
||||
//db, _ = gdb.Instance()
|
||||
}
|
||||
|
||||
// 创建测试数据库
|
||||
func create() error {
|
||||
fmt.Println("drop table aa_user:")
|
||||
_, err := db.Exec("drop table aa_user")
|
||||
if err != nil {
|
||||
fmt.Println("drop table aa_user error.", err)
|
||||
}
|
||||
|
||||
s := `
|
||||
CREATE TABLE aa_user (
|
||||
id int not null,
|
||||
name VARCHAR(60),
|
||||
age int,
|
||||
addr varchar(60),
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
`
|
||||
fmt.Println("create table aa_user:")
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println("create table error.", err)
|
||||
return err
|
||||
}
|
||||
|
||||
/*_, err = db.Exec("drop sequence id_seq")
|
||||
if err != nil {
|
||||
fmt.Println("drop sequence id_seq", err)
|
||||
}
|
||||
|
||||
fmt.Println("create sequence id_seq")
|
||||
_, err = db.Exec("create sequence id_seq increment by 1 start with 1 maxvalue 9999999999 cycle cache 10")
|
||||
if err != nil {
|
||||
fmt.Println("create sequence id_seq error.", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s = `
|
||||
CREATE TRIGGER id_trigger before insert on aa_user for each row
|
||||
begin
|
||||
select id_seq.nextval into :new.id from dual;
|
||||
end;
|
||||
`
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println("create trigger error.", err)
|
||||
return err
|
||||
}*/
|
||||
|
||||
_, err = db.Exec("drop table user_detail")
|
||||
if err != nil {
|
||||
fmt.Println("drop table user_detail", err)
|
||||
}
|
||||
|
||||
s = `
|
||||
CREATE TABLE user_detail (
|
||||
id int not null,
|
||||
site VARCHAR(255),
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
`
|
||||
fmt.Println("create table user_detail:")
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println("create table user_detail error.", err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("create table success.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 数据写入
|
||||
func insert(id int) {
|
||||
fmt.Println("insert:")
|
||||
r, err := db.Insert("aa_user", gdb.Map{
|
||||
"id": id,
|
||||
"name": "john",
|
||||
"age": id,
|
||||
})
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
if err == nil {
|
||||
r, err = db.Insert("user_detail", gdb.Map{
|
||||
"id": id,
|
||||
"site": "http://johng.cn",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Printf("id: %d\n", id)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 基本sql查询
|
||||
func query() {
|
||||
fmt.Println("query:")
|
||||
list, err := db.GetAll("select * from aa_user where 1=1")
|
||||
if err == nil {
|
||||
fmt.Println(list)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
list, err = db.Table("aa_user").OrderBy("id").Limit(0, 5).Select()
|
||||
if err == nil {
|
||||
fmt.Println(list)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// replace into
|
||||
func replace() {
|
||||
fmt.Println("replace:")
|
||||
r, err := db.Save("aa_user", gdb.Map{
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据保存
|
||||
func save() {
|
||||
fmt.Println("save:")
|
||||
r, err := db.Save("aa_user", gdb.Map{
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 批量写入
|
||||
func batchInsert() {
|
||||
fmt.Println("batchInsert:")
|
||||
_, err := db.BatchInsert("aa_user", gdb.List{
|
||||
{"id": 11, "name": "batchInsert_john_1", "age": 11},
|
||||
{"id": 12, "name": "batchInsert_john_2", "age": 12},
|
||||
{"id": 13, "name": "batchInsert_john_3", "age": 13},
|
||||
{"id": 14, "name": "batchInsert_john_4", "age": 14},
|
||||
}, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update1() {
|
||||
fmt.Println("update1:")
|
||||
r, err := db.Update("aa_user", gdb.Map{"name": "john1", "age": 1}, "id=?", 1)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update2() {
|
||||
fmt.Println("update2:")
|
||||
r, err := db.Update("aa_user", gdb.Map{"name": "john6", "age": 6}, "id=?", 2)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update3() {
|
||||
fmt.Println("update3:")
|
||||
r, err := db.Update("aa_user", "name=?", "id=?", "john2", 3)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作1
|
||||
func linkopSelect1() {
|
||||
fmt.Println("linkopSelect1:")
|
||||
r, err := db.Table("aa_user u").LeftJoin("user_detail ud", "u.id=ud.id").Fields("u.*, ud.site").Where("u.id > ?", 1).Limit(3, 5).Select()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作2
|
||||
func linkopSelect2() {
|
||||
fmt.Println("linkopSelect2:")
|
||||
r, err := db.Table("aa_user u").LeftJoin("user_detail ud", "u.id=ud.id").Fields("u.*,ud.site").Where("u.id=?", 1).One()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作3
|
||||
func linkopSelect3() {
|
||||
fmt.Println("linkopSelect3:")
|
||||
r, err := db.Table("aa_user u").LeftJoin("user_detail ud", "u.id=ud.id").Fields("ud.site").Where("u.id=?", 1).Value()
|
||||
if err == nil {
|
||||
fmt.Println(r.String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询数量1
|
||||
func linkopCount1() {
|
||||
fmt.Println("linkopCount1:")
|
||||
r, err := db.Table("aa_user u").LeftJoin("user_detail ud", "u.id=ud.id").Where("name like ?", "john").Count()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 错误操作
|
||||
func linkopUpdate1() {
|
||||
fmt.Println("linkopUpdate1:")
|
||||
r, err := db.Table("henghe_setting").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println("error", err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过Map指针方式传参方式
|
||||
func linkopUpdate2() {
|
||||
fmt.Println("linkopUpdate2:")
|
||||
r, err := db.Table("aa_user").Data(gdb.Map{"name": "john2"}).Where("name=?", "john").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过字符串方式传参
|
||||
func linkopUpdate3() {
|
||||
fmt.Println("linkopUpdate3:")
|
||||
r, err := db.Table("aa_user").Data("name='john3'").Where("name=?", "john2").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// Where条件使用Map
|
||||
func linkopUpdate4() {
|
||||
fmt.Println("linkopUpdate4:")
|
||||
r, err := db.Table("aa_user").Data(gdb.Map{"name": "john11111"}).Where(g.Map{"id": 1}).Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量写入
|
||||
func linkopBatchInsert1() {
|
||||
fmt.Println("linkopBatchInsert1:")
|
||||
r, err := db.Table("aa_user").Filter().Data(gdb.List{
|
||||
{"id": 21, "name": "linkopBatchInsert1_john_1", "amt": 21.21, "tt": "haha"},
|
||||
{"id": 22, "name": "linkopBatchInsert1_john_2", "amt": 22.22, "cc": "hahacc"},
|
||||
{"id": 23, "name": "linkopBatchInsert1_john_3", "amt": 23.23, "bb": "hahabb"},
|
||||
{"id": 24, "name": "linkopBatchInsert1_john_4", "amt": 24.24, "aa": "hahaaa"},
|
||||
}).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量写入,指定每批次写入的条数
|
||||
func linkopBatchInsert2() {
|
||||
fmt.Println("linkopBatchInsert2:")
|
||||
r, err := db.Table("aa_user").Data(gdb.List{
|
||||
{"id": 25, "name": "linkopBatchInsert2john_1"},
|
||||
{"id": 26, "name": "linkopBatchInsert2john_2"},
|
||||
{"id": 27, "name": "linkopBatchInsert2john_3"},
|
||||
{"id": 28, "name": "linkopBatchInsert2john_4"},
|
||||
}).Batch(2).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量保存
|
||||
func linkopBatchSave() {
|
||||
fmt.Println("linkopBatchSave:")
|
||||
r, err := db.Table("aa_user").Data(gdb.List{
|
||||
{"id": 1, "name": "john_1"},
|
||||
{"id": 2, "name": "john_2"},
|
||||
{"id": 3, "name": "john_3"},
|
||||
{"id": 4, "name": "john_4"},
|
||||
}).Save()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 事务操作示例1
|
||||
func transaction1() {
|
||||
fmt.Println("transaction1:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Insert("aa_user", gdb.Map{
|
||||
"id": 30,
|
||||
"name": "transaction1",
|
||||
})
|
||||
tx.Rollback()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 事务操作示例2
|
||||
func transaction2() {
|
||||
fmt.Println("transaction2:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Table("user_detail").Data(gdb.Map{"id": 6, "site": "www.baidu.com哈哈哈*?''\"~!@#$%^&*()"}).Insert()
|
||||
tx.Commit()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 主从io复用测试,在mysql中使用 show full processlist 查看链接信息
|
||||
func keepPing() {
|
||||
fmt.Println("keepPing:")
|
||||
for i := 0; i < 30; i++ {
|
||||
fmt.Println("ping...", i)
|
||||
err := db.PingMaster()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = db.PingSlave()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// like语句查询
|
||||
func likeQuery() {
|
||||
fmt.Println("likeQuery:")
|
||||
if r, err := db.Table("aa_user").Where("name like ?", "%john%").Select(); err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// mapToStruct
|
||||
func mapToStruct() {
|
||||
type User struct {
|
||||
Id int
|
||||
Name string
|
||||
Age int
|
||||
Addr string
|
||||
}
|
||||
fmt.Println("mapToStruct:")
|
||||
if r, err := db.Table("aa_user").Where("id=?", 1).One(); err == nil {
|
||||
u := User{}
|
||||
if err := r.ToStruct(&u); err == nil {
|
||||
fmt.Println(r)
|
||||
fmt.Println(u)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
db.PingMaster()
|
||||
db.SetDebug(true)
|
||||
/*err := create()
|
||||
if err != nil {
|
||||
return
|
||||
}*/
|
||||
|
||||
//test1
|
||||
/*for i := 1; i < 5; i++ {
|
||||
insert(i)
|
||||
}*/
|
||||
//insert(2)
|
||||
//query()
|
||||
|
||||
//batchInsert()
|
||||
//query()
|
||||
|
||||
//replace()
|
||||
//save()
|
||||
|
||||
/*update1()
|
||||
update2()
|
||||
update3()
|
||||
*/
|
||||
|
||||
/*linkopSelect1()
|
||||
linkopSelect2()
|
||||
linkopSelect3()
|
||||
linkopCount1()
|
||||
*/
|
||||
|
||||
/*linkopUpdate1()
|
||||
linkopUpdate2()
|
||||
linkopUpdate3()
|
||||
linkopUpdate4()
|
||||
*/
|
||||
|
||||
linkopBatchInsert1()
|
||||
query()
|
||||
//linkopBatchInsert2()
|
||||
|
||||
//transaction1()
|
||||
//transaction2()
|
||||
//
|
||||
//keepPing()
|
||||
//likeQuery()
|
||||
//mapToStruct()
|
||||
//getQueriedSqls()
|
||||
}
|
||||
26
.example/database/gdb/mysql/config.toml
Normal file
26
.example/database/gdb/mysql/config.toml
Normal file
@ -0,0 +1,26 @@
|
||||
|
||||
# MySQL.
|
||||
[database]
|
||||
[database.logger]
|
||||
Level = "all"
|
||||
Stdout = true
|
||||
CtxKeys = ["Trace-Id"]
|
||||
[database.default]
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
|
||||
debug = true
|
||||
|
||||
# Redis.
|
||||
[redis]
|
||||
default = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
|
||||
#[database]
|
||||
# [[database.default]]
|
||||
# type = "mysql"
|
||||
# link = "root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local"
|
||||
#
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
37
.example/database/gdb/mysql/config/gdb.go
Normal file
37
.example/database/gdb/mysql/config/gdb.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var db gdb.DB
|
||||
|
||||
func init() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
MaxOpenConnCount: 100,
|
||||
})
|
||||
db, _ = gdb.New()
|
||||
}
|
||||
|
||||
func main() {
|
||||
wg := sync.WaitGroup{}
|
||||
for i := 0; i < 100000; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
time.Sleep(10 * time.Second)
|
||||
db.Table("user").Where("id=1").All()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
4
.example/database/gdb/mysql/config2.toml
Normal file
4
.example/database/gdb/mysql/config2.toml
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
# MySQL数据库配置
|
||||
[database]
|
||||
link = "mysql:root:8692651@tcp(192.168.1.11:3306)/test"
|
||||
7
.example/database/gdb/mysql/config3.toml
Normal file
7
.example/database/gdb/mysql/config3.toml
Normal file
@ -0,0 +1,7 @@
|
||||
|
||||
# MySQL数据库配置
|
||||
[database]
|
||||
[database.default]
|
||||
link = "mysql:root:8692651@tcp(192.168.1.11:3306)/test"
|
||||
[database.user]
|
||||
link = "mysql:root:8692651@tcp(192.168.1.11:3306)/test"
|
||||
28
.example/database/gdb/mysql/gdb_all.go
Normal file
28
.example/database/gdb/mysql/gdb_all.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.GetAll("SELECT * from `user` where id in(?)", g.Slice{})
|
||||
if e != nil {
|
||||
fmt.Println(e)
|
||||
}
|
||||
if r != nil {
|
||||
fmt.Println(r)
|
||||
}
|
||||
return
|
||||
//r, e := db.Table("user").Where("id in(?)", g.Slice{}).All()
|
||||
//if e != nil {
|
||||
// fmt.Println(e)
|
||||
//}
|
||||
//if r != nil {
|
||||
// fmt.Println(r.List())
|
||||
//}
|
||||
}
|
||||
23
.example/database/gdb/mysql/gdb_args_slice.go
Normal file
23
.example/database/gdb/mysql/gdb_args_slice.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
|
||||
db.Table("user").Where("nickname like ? and passport like ?", g.Slice{"T3", "t3"}).OrderBy("id asc").All()
|
||||
|
||||
conditions := g.Map{
|
||||
"nickname like ?": "%T%",
|
||||
"id between ? and ?": g.Slice{1, 3},
|
||||
"id >= ?": 1,
|
||||
"create_time > ?": 0,
|
||||
"id in(?)": g.Slice{1, 2, 3},
|
||||
}
|
||||
db.Table("user").Where(conditions).OrderBy("id asc").All()
|
||||
|
||||
var params []interface{}
|
||||
db.Table("user").Where("1=1", params).OrderBy("id asc").All()
|
||||
}
|
||||
24
.example/database/gdb/mysql/gdb_batch_insert.go
Normal file
24
.example/database/gdb/mysql/gdb_batch_insert.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
list := make(g.List, 0)
|
||||
for i := 0; i < 100; i++ {
|
||||
list = append(list, g.Map{
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
}
|
||||
r, e := db.Table("user").Data(list).Batch(2).Insert()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if r != nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
}
|
||||
}
|
||||
51
.example/database/gdb/mysql/gdb_binary.go
Normal file
51
.example/database/gdb/mysql/gdb_binary.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/crypto/gaes"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "123456",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
key := "0123456789123456"
|
||||
|
||||
name := "john"
|
||||
encryptedName, err := gaes.Encrypt([]byte(name), []byte(key))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// 写入
|
||||
r, err := db.Table("user").Data(g.Map{
|
||||
"uid": 1,
|
||||
"name": encryptedName,
|
||||
}).Save()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(r.RowsAffected())
|
||||
|
||||
// 查询
|
||||
one, err := db.Table("user").Where("name=?", encryptedName).One()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(one.ToMap())
|
||||
}
|
||||
20
.example/database/gdb/mysql/gdb_bit.go
Normal file
20
.example/database/gdb/mysql/gdb_bit.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("test").All()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if r != nil {
|
||||
fmt.Println(r.ToList())
|
||||
}
|
||||
}
|
||||
40
.example/database/gdb/mysql/gdb_cache.go
Normal file
40
.example/database/gdb/mysql/gdb_cache.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//db.GetCache().SetAdapter(adapter.NewRedis(g.Redis()))
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
// 执行2次查询并将查询结果缓存3秒,并可执行缓存名称(可选)
|
||||
for i := 0; i < 3; i++ {
|
||||
r, _ := db.Table("user").Cache(3000*time.Second).Where("id=?", 1).One()
|
||||
gutil.Dump(r.Map())
|
||||
}
|
||||
|
||||
// 执行更新操作,并清理指定名称的查询缓存
|
||||
//db.Table("user").Cache(-1, "vip-user").Data(gdb.Map{"name": "smith"}).Where("id=?", 1).Update()
|
||||
|
||||
// 再次执行查询,启用查询缓存特性
|
||||
//r, _ := db.Table("user").Cache(300000*time.Second, "vip-user").Where("id=?", 1).One()
|
||||
//gutil.Dump(r.Map())
|
||||
}
|
||||
24
.example/database/gdb/mysql/gdb_complecated.go
Normal file
24
.example/database/gdb/mysql/gdb_complecated.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// error!
|
||||
r, err := g.DB().Table("user").Where(g.Map{
|
||||
"or": g.Map{
|
||||
"nickname": "jim",
|
||||
"create_time > ": "2019-10-01",
|
||||
},
|
||||
"and": g.Map{
|
||||
"nickname": "tom",
|
||||
"create_time > ": "2019-10-01",
|
||||
},
|
||||
}).All()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Dump(r)
|
||||
|
||||
}
|
||||
16
.example/database/gdb/mysql/gdb_config.go
Normal file
16
.example/database/gdb/mysql/gdb_config.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
17
.example/database/gdb/mysql/gdb_config2.go
Normal file
17
.example/database/gdb/mysql/gdb_config2.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g.Config().SetFileName("config2.toml")
|
||||
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
24
.example/database/gdb/mysql/gdb_config3.go
Normal file
24
.example/database/gdb/mysql/gdb_config3.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g.Config().SetFileName("config3.toml")
|
||||
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
if r, err := g.DB("user").Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
14
.example/database/gdb/mysql/gdb_ctx.go
Normal file
14
.example/database/gdb/mysql/gdb_ctx.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
|
||||
_, err := g.DB().Ctx(ctx).Query("SELECT 1")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
14
.example/database/gdb/mysql/gdb_ctx_model.go
Normal file
14
.example/database/gdb/mysql/gdb_ctx_model.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
|
||||
_, err := g.DB().Model("user").Ctx(ctx).All()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
30
.example/database/gdb/mysql/gdb_datetime.go
Normal file
30
.example/database/gdb/mysql/gdb_datetime.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.Database()
|
||||
db.SetDebug(true)
|
||||
|
||||
//r, err := db.Table("user").Data("create_time", gtime.Now().String()).Insert()
|
||||
//if err == nil {
|
||||
// fmt.Println(r.LastInsertId())
|
||||
//} else {
|
||||
// panic(err)
|
||||
//}
|
||||
|
||||
r, err := db.Table("user").Data(g.Map{
|
||||
"name": "john",
|
||||
"create_time": gtime.Now().String(),
|
||||
}).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
37
.example/database/gdb/mysql/gdb_debug1.go
Normal file
37
.example/database/gdb/mysql/gdb_debug1.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//db.SetDebug(false)
|
||||
|
||||
glog.SetPath("/tmp")
|
||||
|
||||
// 执行3条SQL查询
|
||||
for i := 1; i <= 3; i++ {
|
||||
db.Table("user").Where("uid=?", i).One()
|
||||
}
|
||||
// 构造一条错误查询
|
||||
db.Table("user").Where("no_such_field=?", "just_test").One()
|
||||
|
||||
db.Table("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
|
||||
|
||||
}
|
||||
18
.example/database/gdb/mysql/gdb_debug2.go
Normal file
18
.example/database/gdb/mysql/gdb_debug2.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
|
||||
// 执行3条SQL查询
|
||||
for i := 1; i <= 3; i++ {
|
||||
db.Table("user").Where("id=?", i).One()
|
||||
}
|
||||
// 构造一条错误查询
|
||||
db.Table("user").Where("no_such_field=?", "just_test").One()
|
||||
|
||||
db.Table("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
|
||||
}
|
||||
9
.example/database/gdb/mysql/gdb_distinct.go
Normal file
9
.example/database/gdb/mysql/gdb_distinct.go
Normal file
@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g.DB().Model("user").Distinct().CountColumn("uid,name")
|
||||
}
|
||||
30
.example/database/gdb/mysql/gdb_insert.go
Normal file
30
.example/database/gdb/mysql/gdb_insert.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
//db := g.DB()
|
||||
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Link: "root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local",
|
||||
Type: "mysql",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db, _ := gdb.New()
|
||||
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("user").Data(g.Map{
|
||||
"create_at": "now()",
|
||||
}).Unscoped().Insert()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if r != nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
}
|
||||
}
|
||||
50
.example/database/gdb/mysql/gdb_issue_278.go
Normal file
50
.example/database/gdb/mysql/gdb_issue_278.go
Normal file
@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
var (
|
||||
tableName = "orders"
|
||||
dao = g.DB().Table(tableName).Safe()
|
||||
)
|
||||
|
||||
type OrderServiceEntity struct {
|
||||
GoodsPrice float64 `json:"goods_price" gvalid:"required"`
|
||||
PayTo int8 `json:"payTo" gvalid:"required"`
|
||||
PayStatus int8 `json:"payStatus" `
|
||||
CreateTime string `json:"createTime" `
|
||||
AppId string `json:"appId" gvalid:"required"`
|
||||
PayUser string `json:"pay_user" gvalid:"required"`
|
||||
QrUrl string `json:"qr_url" `
|
||||
}
|
||||
|
||||
type Create struct {
|
||||
Id int64 `json:"id" gconv:"id"`
|
||||
GoodsPrice float64 `json:"goodsPrice" gconv:"goods_price"`
|
||||
PayTo int8 `json:"payTo" gconv:"pay_to"`
|
||||
PayStatus int8 `json:"payStatus" gconv:"pay_status"`
|
||||
CreateTime string `json:"createTime" gconv:"create_time"`
|
||||
UserId int `json:"user_id" `
|
||||
PayUser string `json:"pay_user" `
|
||||
QrUrl string `json:"qr_url" `
|
||||
}
|
||||
|
||||
func main() {
|
||||
g.DB().SetDebug(true)
|
||||
userInfo := Create{
|
||||
Id: 3,
|
||||
}
|
||||
orderService := OrderServiceEntity{
|
||||
GoodsPrice: 0.1,
|
||||
PayTo: 1,
|
||||
}
|
||||
size, err := dao.Where("user_id", userInfo.Id).
|
||||
And("goods_price", float64(100.10)).
|
||||
And("pay_status", 0).
|
||||
And("pay_to", orderService.PayTo).Count()
|
||||
fmt.Println(err)
|
||||
fmt.Println(size)
|
||||
}
|
||||
37
.example/database/gdb/mysql/gdb_json_xml.go
Normal file
37
.example/database/gdb/mysql/gdb_json_xml.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/encoding/gparser"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3306",
|
||||
User: "root",
|
||||
Pass: "12345678",
|
||||
Name: "test",
|
||||
Type: "mysql",
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db := g.DB()
|
||||
one, err := db.Table("user").Where("id=?", 1).One()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 使用内置方法转换为json/xml
|
||||
fmt.Println(one.ToJson())
|
||||
fmt.Println(one.ToXml())
|
||||
|
||||
// 自定义方法方法转换为json/xml
|
||||
jsonContent, _ := gparser.VarToJson(one.ToMap())
|
||||
fmt.Println(string(jsonContent))
|
||||
xmlContent, _ := gparser.VarToXml(one.ToMap())
|
||||
fmt.Println(string(xmlContent))
|
||||
}
|
||||
22
.example/database/gdb/mysql/gdb_pool.go
Normal file
22
.example/database/gdb/mysql/gdb_pool.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
for {
|
||||
for i := 0; i < 10; i++ {
|
||||
go db.Table("user").All()
|
||||
}
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
|
||||
}
|
||||
18
.example/database/gdb/mysql/gdb_reconnect.go
Normal file
18
.example/database/gdb/mysql/gdb_reconnect.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
for {
|
||||
r, err := db.Table("user").All()
|
||||
fmt.Println(err)
|
||||
fmt.Println(r)
|
||||
time.Sleep(time.Second * 10)
|
||||
}
|
||||
}
|
||||
23
.example/database/gdb/mysql/gdb_struct.go
Normal file
23
.example/database/gdb/mysql/gdb_struct.go
Normal file
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
type User struct {
|
||||
Uid int
|
||||
Name string
|
||||
}
|
||||
user := (*User)(nil)
|
||||
fmt.Println(user)
|
||||
err := db.Table("test").Where("id=1").Struct(&user)
|
||||
fmt.Println(err)
|
||||
fmt.Println(user)
|
||||
}
|
||||
18
.example/database/gdb/mysql/gdb_tables.go
Normal file
18
.example/database/gdb/mysql/gdb_tables.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
|
||||
tables, err := db.Tables()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if tables != nil {
|
||||
g.Dump(tables)
|
||||
}
|
||||
}
|
||||
25
.example/database/gdb/mysql/gdb_tables_fields.go
Normal file
25
.example/database/gdb/mysql/gdb_tables_fields.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
|
||||
tables, e := db.Tables()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if tables != nil {
|
||||
g.Dump(tables)
|
||||
for _, table := range tables {
|
||||
fields, err := db.TableFields(table)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Dump(fields)
|
||||
}
|
||||
}
|
||||
}
|
||||
27
.example/database/gdb/mysql/gdb_transaction.go
Normal file
27
.example/database/gdb/mysql/gdb_transaction.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
db = g.DB()
|
||||
table = "user"
|
||||
)
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err = tx.Begin(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = tx.Model(table).Data(g.Map{"id": 1, "name": "john"}).Insert()
|
||||
if err = tx.Rollback(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = tx.Model(table).Data(g.Map{"id": 2, "name": "smith"}).Insert()
|
||||
if err = tx.Commit(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
34
.example/database/gdb/mysql/gdb_transaction_closure.go
Normal file
34
.example/database/gdb/mysql/gdb_transaction_closure.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
err error
|
||||
db = g.DB()
|
||||
table = "user"
|
||||
)
|
||||
if err = db.Transaction(func(tx *gdb.TX) error {
|
||||
// Nested transaction 1.
|
||||
if err = tx.Transaction(func(tx *gdb.TX) error {
|
||||
_, err = tx.Model(table).Data(g.Map{"id": 1, "name": "john"}).Insert()
|
||||
return err
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
// Nested transaction 2, panic.
|
||||
if err = tx.Transaction(func(tx *gdb.TX) error {
|
||||
_, err = tx.Model(table).Data(g.Map{"id": 2, "name": "smith"}).Insert()
|
||||
// Create a panic that can make this transaction rollback automatically.
|
||||
panic("error")
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
40
.example/database/gdb/mysql/gdb_transaction_savepoint.go
Normal file
40
.example/database/gdb/mysql/gdb_transaction_savepoint.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
err error
|
||||
db = g.DB()
|
||||
table = "user"
|
||||
)
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
if _, err = tx.Model(table).Data(g.Map{"id": 1, "name": "john"}).Insert(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err = tx.SavePoint("MyPoint"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if _, err = tx.Model(table).Data(g.Map{"id": 2, "name": "smith"}).Insert(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if _, err = tx.Model(table).Data(g.Map{"id": 3, "name": "green"}).Insert(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err = tx.RollbackTo("MyPoint"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
34
.example/database/gdb/mysql/gdb_update_field.go
Normal file
34
.example/database/gdb/mysql/gdb_update_field.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
table := "medicine_clinics_upload_yinchuan"
|
||||
list, err := db.Table(table).All()
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
panic(err)
|
||||
}
|
||||
content := ""
|
||||
for _, item := range list {
|
||||
if j, err := gjson.DecodeToJson(item["upload_data"].String()); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
s, _ := j.ToJsonIndentString()
|
||||
content += item["id"].String() + "\t" + item["medicine_clinic_id"].String() + "\t"
|
||||
content += s
|
||||
content += "\n\n"
|
||||
//if _, err := db.Table(table).Data("data_decode", s).Where("id", item["id"].Int()).Update(); err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
}
|
||||
}
|
||||
gfile.PutContents("/Users/john/Temp/medicine_clinics_upload_yinchuan.txt", content)
|
||||
}
|
||||
17
.example/database/gdb/mysql/gdb_update_union.go
Normal file
17
.example/database/gdb/mysql/gdb_update_union.go
Normal file
@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
result, err := db.Table("pw_passageway m,pw_template t").Data("t.status", 99).Where("m.templateId=t.id AND m.status = 0").Update()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(result.RowsAffected())
|
||||
}
|
||||
15
.example/database/gdb/mysql/gdb_value.go
Normal file
15
.example/database/gdb/mysql/gdb_value.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
one, err := g.DB().Table("carlist c").
|
||||
LeftJoin("cardetail d", "c.postid=d.carid").
|
||||
Where("c.postid", "142039140032006").
|
||||
Fields("c.*,d.*").One()
|
||||
fmt.Println(err)
|
||||
g.Dump(one)
|
||||
}
|
||||
66
.example/database/gdb/mysql/gdb_with_insert.go
Normal file
66
.example/database/gdb/mysql/gdb_with_insert.go
Normal file
@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gmeta"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type UserDetail struct {
|
||||
gmeta.Meta `orm:"table:user_detail"`
|
||||
Uid int `json:"uid"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
type UserScore struct {
|
||||
gmeta.Meta `orm:"table:user_score"`
|
||||
Id int `json:"id"`
|
||||
Uid int `json:"uid"`
|
||||
Score int `json:"score"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gmeta.Meta `orm:"table:user"`
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
UserDetail *UserDetail `orm:"with:uid=id"`
|
||||
UserScores []*UserScore `orm:"with:uid=id"`
|
||||
}
|
||||
|
||||
db := g.DB()
|
||||
db.Transaction(func(tx *gdb.TX) error {
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
user := User{
|
||||
Name: fmt.Sprintf(`name_%d`, i),
|
||||
}
|
||||
lastInsertId, err := db.Model(user).Data(user).OmitEmpty().InsertAndGetId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Detail.
|
||||
userDetail := UserDetail{
|
||||
Uid: int(lastInsertId),
|
||||
Address: fmt.Sprintf(`address_%d`, lastInsertId),
|
||||
}
|
||||
_, err = db.Model(userDetail).Data(userDetail).OmitEmpty().Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
userScore := UserScore{
|
||||
Uid: int(lastInsertId),
|
||||
Score: j,
|
||||
}
|
||||
_, err = db.Model(userScore).Data(userScore).OmitEmpty().Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
37
.example/database/gdb/mysql/gdb_with_slect.go
Normal file
37
.example/database/gdb/mysql/gdb_with_slect.go
Normal file
@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gmeta"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type UserDetail struct {
|
||||
gmeta.Meta `orm:"table:user_detail"`
|
||||
Uid int `json:"uid"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
type UserScore struct {
|
||||
gmeta.Meta `orm:"table:user_score"`
|
||||
Id int `json:"id"`
|
||||
Uid int `json:"uid"`
|
||||
Score int `json:"score"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gmeta.Meta `orm:"table:user"`
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
UserDetail *UserDetail `orm:"with:uid=id"`
|
||||
UserScores []*UserScore `orm:"with:uid=id"`
|
||||
}
|
||||
|
||||
db := g.DB()
|
||||
var user *User
|
||||
err := db.Model(user).WithAll().Where("id", 3).Scan(&user)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Dump(user)
|
||||
}
|
||||
34
.example/database/gdb/mysql/issue364.go
Normal file
34
.example/database/gdb/mysql/issue364.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"time"
|
||||
)
|
||||
|
||||
func test1() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
time.Sleep(1 * time.Minute)
|
||||
r, e := db.Table("test").Where("id", 10000).Count()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
g.Dump(r)
|
||||
}
|
||||
|
||||
func test2() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
dao := db.Table("test").Safe()
|
||||
time.Sleep(1 * time.Minute)
|
||||
r, e := dao.Where("id", 10000).Count()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
g.Dump(r)
|
||||
}
|
||||
|
||||
func main() {
|
||||
test1()
|
||||
test2()
|
||||
}
|
||||
565
.example/database/gdb/oracle/gdb.go
Normal file
565
.example/database/gdb/oracle/gdb.go
Normal file
@ -0,0 +1,565 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
//_ "github.com/mattn/go-oci8"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
// 本文件用于gf框架的mysql数据库操作示例,不作为单元测试使用
|
||||
|
||||
var db gdb.DB
|
||||
|
||||
// 初始化配置及创建数据库
|
||||
func init() {
|
||||
gdb.AddDefaultConfigNode(gdb.ConfigNode{
|
||||
Host: "192.168.146.0",
|
||||
Port: "1521",
|
||||
User: "test",
|
||||
Pass: "test",
|
||||
Name: "orcl",
|
||||
Type: "oracle",
|
||||
Role: "master",
|
||||
})
|
||||
db, _ = gdb.New()
|
||||
|
||||
//gins.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/frame")
|
||||
//db = g.Database()
|
||||
|
||||
//gdb.SetConfig(gdb.ConfigNode {
|
||||
// Host : "127.0.0.1",
|
||||
// Port : 3306,
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
//})
|
||||
//db, _ = gdb.Instance()
|
||||
|
||||
//gdb.SetConfig(gdb.Config {
|
||||
// "default" : gdb.ConfigGroup {
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.1",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.2",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.3",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// gdb.ConfigNode {
|
||||
// Host : "127.0.0.4",
|
||||
// Port : "3306",
|
||||
// User : "root",
|
||||
// Pass : "123456",
|
||||
// Name : "test",
|
||||
// Type : "mysql",
|
||||
// Role : "master",
|
||||
// Weight : 100,
|
||||
// },
|
||||
// },
|
||||
//})
|
||||
//db, _ = gdb.Instance()
|
||||
}
|
||||
|
||||
// 创建测试数据库
|
||||
func create() error {
|
||||
fmt.Println("drop table aa_user:")
|
||||
_, err := db.Exec("drop table aa_user")
|
||||
if err != nil {
|
||||
fmt.Println("drop table aa_user error.", err)
|
||||
}
|
||||
|
||||
s := `
|
||||
CREATE TABLE aa_user (
|
||||
id number(10) not null,
|
||||
name VARCHAR2(45),
|
||||
age number(8),
|
||||
addr varchar2(60),
|
||||
amt number(12,2),
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
`
|
||||
fmt.Println("create table aa_user:")
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println("create table error.", err)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.Exec("drop sequence id_seq")
|
||||
if err != nil {
|
||||
fmt.Println("drop sequence id_seq", err)
|
||||
}
|
||||
|
||||
/*fmt.Println("create sequence id_seq")
|
||||
_, err = db.Exec("create sequence id_seq increment by 1 start with 1 maxvalue 9999999999 cycle cache 10")
|
||||
if err != nil {
|
||||
fmt.Println("create sequence id_seq error.", err)
|
||||
return err
|
||||
}
|
||||
|
||||
s = `
|
||||
CREATE TRIGGER id_trigger before insert on aa_user for each row
|
||||
begin
|
||||
select id_seq.nextval into :new.id from dual;
|
||||
end;
|
||||
`
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println("create trigger error.", err)
|
||||
return err
|
||||
}*/
|
||||
|
||||
_, err = db.Exec("drop table user_detail")
|
||||
if err != nil {
|
||||
fmt.Println("drop table user_detail", err)
|
||||
}
|
||||
|
||||
s = `
|
||||
CREATE TABLE user_detail (
|
||||
id number(10) not null,
|
||||
site VARCHAR2(255),
|
||||
PRIMARY KEY (id)
|
||||
)
|
||||
`
|
||||
fmt.Println("create table user_detail:")
|
||||
_, err = db.Exec(s)
|
||||
if err != nil {
|
||||
fmt.Println("create table user_detail error.", err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("create table success.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 数据写入
|
||||
func insert(id int) {
|
||||
fmt.Println("insert:")
|
||||
|
||||
r, err := db.Insert("aa_user", gdb.Map{
|
||||
"id": id,
|
||||
"name": "john",
|
||||
"age": id,
|
||||
})
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
if err == nil {
|
||||
r, err = db.Insert("user_detail", gdb.Map{
|
||||
"id": id,
|
||||
"site": "http://johng.cn",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Printf("id: %d\n", id)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 基本sql查询
|
||||
func query() {
|
||||
fmt.Println("query:")
|
||||
list, err := db.GetAll("select * from aa_user")
|
||||
if err == nil {
|
||||
fmt.Println(list)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
list, err = db.Table("aa_user").OrderBy("id").Limit(0, 2).Select()
|
||||
if err == nil {
|
||||
fmt.Println(list)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// replace into
|
||||
func replace() {
|
||||
fmt.Println("replace:")
|
||||
r, err := db.Save("aa_user", gdb.Map{
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据保存
|
||||
func save() {
|
||||
fmt.Println("save:")
|
||||
r, err := db.Save("aa_user", gdb.Map{
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 批量写入
|
||||
func batchInsert() {
|
||||
fmt.Println("batchInsert:")
|
||||
_, err := db.BatchInsert("aa_user", gdb.List{
|
||||
{"id": 11, "name": "batchInsert_john_1", "age": 11, "amt": 11.11},
|
||||
{"id": 12, "name": "batchInsert_john_2", "age": 12, "amt": 12.12},
|
||||
{"id": 13, "name": "batchInsert_john_3", "age": 13, "amt": 13.13},
|
||||
{"id": 14, "name": "batchInsert_john_4", "age": 14, "amt": 14.14},
|
||||
}, 10)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update1() {
|
||||
fmt.Println("update1:")
|
||||
r, err := db.Update("aa_user", gdb.Map{"name": "john1", "age": 1}, "id=?", 1)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update2() {
|
||||
fmt.Println("update2:")
|
||||
r, err := db.Update("aa_user", gdb.Map{"name": "john6", "age": 6}, "id=?", 2)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 数据更新
|
||||
func update3() {
|
||||
fmt.Println("update3:")
|
||||
r, err := db.Update("aa_user", "name=?", "id=?", "john2", 3)
|
||||
if err == nil {
|
||||
fmt.Println(r.LastInsertId())
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作1
|
||||
func linkopSelect1() {
|
||||
fmt.Println("linkopSelect1:")
|
||||
r, err := db.Table("aa_user u").LeftJoin("user_detail ud", "u.id=ud.id").Fields("u.*, ud.site").Where("u.id > ?", 1).Limit(0, 2).Select()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作2
|
||||
func linkopSelect2() {
|
||||
fmt.Println("linkopSelect2:")
|
||||
r, err := db.Table("aa_user u").LeftJoin("user_detail ud", "u.id=ud.id").Fields("u.*,ud.site").Where("u.id=?", 1).One()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询操作3
|
||||
func linkopSelect3() {
|
||||
fmt.Println("linkopSelect3:")
|
||||
r, err := db.Table("aa_user u").LeftJoin("user_detail ud", "u.id=ud.id").Fields("ud.site").Where("u.id=?", 1).Value()
|
||||
if err == nil {
|
||||
fmt.Println(r.String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式查询数量1
|
||||
func linkopCount1() {
|
||||
fmt.Println("linkopCount1:")
|
||||
r, err := db.Table("aa_user u").LeftJoin("user_detail ud", "u.id=ud.id").Where("name like ?", "john").Count()
|
||||
if err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 错误操作
|
||||
func linkopUpdate1() {
|
||||
fmt.Println("linkopUpdate1:")
|
||||
r, err := db.Table("henghe_setting").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println("error", err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过Map指针方式传参方式
|
||||
func linkopUpdate2() {
|
||||
fmt.Println("linkopUpdate2:")
|
||||
r, err := db.Table("aa_user").Data(gdb.Map{"name": "john2"}).Where("name=?", "john").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 通过字符串方式传参
|
||||
func linkopUpdate3() {
|
||||
fmt.Println("linkopUpdate3:")
|
||||
r, err := db.Table("aa_user").Data("name='john3'").Where("name=?", "john2").Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// Where条件使用Map
|
||||
func linkopUpdate4() {
|
||||
fmt.Println("linkopUpdate4:")
|
||||
r, err := db.Table("aa_user").Data(gdb.Map{"name": "john11111"}).Where(g.Map{"id": 1}).Update()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量写入
|
||||
func linkopBatchInsert1() {
|
||||
fmt.Println("linkopBatchInsert1:")
|
||||
r, err := db.Table("aa_user").Filter().Data(gdb.List{
|
||||
{"id": 21, "name": "linkopBatchInsert1_john_1", "amt": 21.21, "tt": "haha"},
|
||||
{"id": 22, "name": "linkopBatchInsert1_john_2", "amt": 22.22, "cc": "hahacc"},
|
||||
{"id": 23, "name": "linkopBatchInsert1_john_3", "amt": 23.23, "bb": "hahabb"},
|
||||
{"id": 24, "name": "linkopBatchInsert1_john_4", "amt": 24.24, "aa": "hahaaa"},
|
||||
}).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量写入,指定每批次写入的条数
|
||||
func linkopBatchInsert2() {
|
||||
fmt.Println("linkopBatchInsert2:")
|
||||
r, err := db.Table("aa_user").Data(gdb.List{
|
||||
{"id": 25, "name": "linkopBatchInsert2john_1"},
|
||||
{"id": 26, "name": "linkopBatchInsert2john_2"},
|
||||
{"id": 27, "name": "linkopBatchInsert2john_3"},
|
||||
{"id": 28, "name": "linkopBatchInsert2john_4"},
|
||||
}).Batch(2).Insert()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 链式批量保存
|
||||
func linkopBatchSave() {
|
||||
fmt.Println("linkopBatchSave:")
|
||||
r, err := db.Table("aa_user").Data(gdb.List{
|
||||
{"id": 1, "name": "john_1"},
|
||||
{"id": 2, "name": "john_2"},
|
||||
{"id": 3, "name": "john_3"},
|
||||
{"id": 4, "name": "john_4"},
|
||||
}).Save()
|
||||
if err == nil {
|
||||
fmt.Println(r.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 事务操作示例1
|
||||
func transaction1() {
|
||||
fmt.Println("transaction1:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Insert("aa_user", gdb.Map{
|
||||
"id": 30,
|
||||
"name": "transaction1",
|
||||
})
|
||||
tx.Rollback()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 事务操作示例2
|
||||
func transaction2() {
|
||||
fmt.Println("transaction2:")
|
||||
if tx, err := db.Begin(); err == nil {
|
||||
r, err := tx.Table("user_detail").Data(gdb.Map{"id": 5, "site": "www.baidu.com哈哈哈*?~!@#$%^&*()"}).Insert()
|
||||
tx.Commit()
|
||||
fmt.Println(r, err)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 主从io复用测试,在mysql中使用 show full processlist 查看链接信息
|
||||
func keepPing() {
|
||||
fmt.Println("keepPing:")
|
||||
for i := 0; i < 30; i++ {
|
||||
fmt.Println("ping...", i)
|
||||
err := db.PingMaster()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = db.PingSlave()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// like语句查询
|
||||
func likeQuery() {
|
||||
fmt.Println("likeQuery:")
|
||||
if r, err := db.Table("aa_user").Where("name like ?", "%john%").Select(); err == nil {
|
||||
fmt.Println(r)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
// mapToStruct
|
||||
func mapToStruct() {
|
||||
type User struct {
|
||||
Id int
|
||||
Name string
|
||||
Age int
|
||||
Addr string
|
||||
}
|
||||
fmt.Println("mapToStruct:")
|
||||
if r, err := db.Table("aa_user").Where("id=?", 1).One(); err == nil {
|
||||
u := User{}
|
||||
if err := r.ToStruct(&u); err == nil {
|
||||
fmt.Println(r)
|
||||
fmt.Println(u)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
db.PingMaster()
|
||||
db.SetDebug(true)
|
||||
/*err := create()
|
||||
if err != nil {
|
||||
return
|
||||
}*/
|
||||
|
||||
//test1
|
||||
/*for i := 1; i < 5; i++ {
|
||||
insert(i)
|
||||
}
|
||||
query()
|
||||
*/
|
||||
|
||||
//batchInsert()
|
||||
//query()
|
||||
|
||||
//replace()
|
||||
//save()
|
||||
|
||||
//update1()
|
||||
//update2()
|
||||
//update3()
|
||||
|
||||
/*linkopSelect1()
|
||||
linkopSelect2()
|
||||
linkopSelect3()
|
||||
linkopCount1()
|
||||
*/
|
||||
|
||||
/*linkopUpdate1()
|
||||
linkopUpdate2()
|
||||
linkopUpdate3()
|
||||
linkopUpdate4()
|
||||
*/
|
||||
|
||||
linkopBatchInsert1()
|
||||
query()
|
||||
//linkopBatchInsert2()
|
||||
|
||||
//transaction1()
|
||||
//transaction2()
|
||||
//
|
||||
//keepPing()
|
||||
//likeQuery()
|
||||
//mapToStruct()
|
||||
//getQueriedSqls()
|
||||
}
|
||||
47
.example/database/gdb/sqlite/sqlite.go
Normal file
47
.example/database/gdb/sqlite/sqlite.go
Normal file
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gdb.SetConfig(gdb.Config{
|
||||
"default": gdb.ConfigGroup{
|
||||
gdb.ConfigNode{
|
||||
Name: "/tmp/my.db",
|
||||
Type: "sqlite",
|
||||
},
|
||||
},
|
||||
})
|
||||
db := g.DB()
|
||||
if db == nil {
|
||||
panic("db create failed")
|
||||
}
|
||||
|
||||
// 创建表
|
||||
sql := `CREATE TABLE user (
|
||||
uid INT PRIMARY KEY NOT NULL,
|
||||
name VARCHAR(30) NOT NULL
|
||||
);`
|
||||
if _, err := db.Exec(sql); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
result, err := db.Table("user").Data(g.Map{"uid": 1, "name": "john"}).Save()
|
||||
if err == nil {
|
||||
fmt.Println(result.RowsAffected())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// 删除表
|
||||
sql = `DROP TABLE user;`
|
||||
if _, err := db.Exec(sql); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
4
.example/database/gredis/config.toml
Normal file
4
.example/database/gredis/config.toml
Normal file
@ -0,0 +1,4 @@
|
||||
# Redis数据库配置
|
||||
[redis]
|
||||
default = "127.0.0.1:6379,0"
|
||||
cache = "127.0.0.1:6379,1"
|
||||
21
.example/database/gredis/gredis.go
Normal file
21
.example/database/gredis/gredis.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/database/gredis"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// 使用原生gredis.New操作redis,但是注意需要自己调用Close方法关闭redis链接池
|
||||
func main() {
|
||||
config := &gredis.Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: 6379,
|
||||
}
|
||||
redis := gredis.New(config)
|
||||
defer redis.Close()
|
||||
redis.Do("SET", "k", "v")
|
||||
v, _ := redis.Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
}
|
||||
15
.example/database/gredis/gredis2.go
Normal file
15
.example/database/gredis/gredis2.go
Normal file
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// 使用框架封装的g.Redis()方法获得redis操作对象单例,不需要开发者显示调用Close方法
|
||||
func main() {
|
||||
g.Redis().Do("SET", "k", "v")
|
||||
v, _ := g.Redis().Do("GET", "k")
|
||||
fmt.Println(gconv.String(v))
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user