A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/grafana/mcp-grafana/commit/aa5bf9539375e3317a3e864a6ed8184bec5be811 below:

Fix commas in JSONSchema descriptions and add linter (#98) · grafana/mcp-grafana@aa5bf95 · GitHub

File tree Expand file treeCollapse file tree 13 files changed

+575

-42

lines changed

Filter options

Expand file treeCollapse file tree 13 files changed

+575

-42

lines changed Original file line number Diff line number Diff line change

@@ -2,7 +2,7 @@ name: Go

2 2 3 3

on:

4 4

push:

5 -

branches: [ main ]

5 +

branches: [main]

6 6

pull_request:

7 7 8 8

jobs:

@@ -16,13 +16,11 @@ jobs:

16 16

- name: Set up Go

17 17

uses: actions/setup-go@v5

18 18

with:

19 -

go-version: '1.24'

19 +

go-version: "1.24"

20 20

cache: true

21 21 22 -

- name: Lint

23 -

uses: golangci/golangci-lint-action@v3

24 -

with:

25 -

version: v1.64

22 +

- name: Run linters

23 +

run: make lint

26 24 27 25

test-unit:

28 26

name: Test Unit

@@ -34,7 +32,7 @@ jobs:

34 32

- name: Set up Go

35 33

uses: actions/setup-go@v5

36 34

with:

37 -

go-version: '1.24'

35 +

go-version: "1.24"

38 36

cache: true

39 37 40 38

- name: Run unit tests

@@ -58,7 +56,7 @@ jobs:

58 56

- name: Set up Go

59 57

uses: actions/setup-go@v5

60 58

with:

61 -

go-version: '1.24'

59 +

go-version: "1.24"

62 60

cache: true

63 61 64 62

- name: Wait for Grafana server and Prometheus server to start and scrape

@@ -77,12 +75,11 @@ jobs:

77 75

- name: Set up Go

78 76

uses: actions/setup-go@v5

79 77

with:

80 -

go-version: '1.24'

78 +

go-version: "1.24"

81 79

cache: true

82 80 83 81

- name: Run cloud tests

84 82

env:

85 83

GRAFANA_URL: ${{ vars.CLOUD_GRAFANA_URL }}

86 84

GRAFANA_API_KEY: ${{ secrets.CLOUD_GRAFANA_API_KEY }}

87 85

run: make test-cloud

88 - Original file line number Diff line number Diff line change

@@ -12,10 +12,16 @@ help: ## Print this help message.

12 12

build-image: ## Build the Docker image.

13 13

docker build -t mcp-grafana:latest .

14 14 15 -

.PHONY: lint

16 -

lint: ## Lint the Go code.

15 +

.PHONY: lint lint-jsonschema lint-jsonschema-fix

16 +

lint: lint-jsonschema ## Lint the Go code.

17 17

go tool -modfile go.tools.mod golangci-lint run

18 18 19 +

lint-jsonschema: ## Lint for unescaped commas in jsonschema tags.

20 +

go run ./cmd/linters/jsonschema --path .

21 + 22 +

lint-jsonschema-fix: ## Automatically fix unescaped commas in jsonschema tags.

23 +

go run ./cmd/linters/jsonschema --path . --fix

24 + 19 25

.PHONY: test test-unit

20 26

test-unit: ## Run the unit tests (no external dependencies required).

21 27

go test -v -tags unit ./...

Original file line number Diff line number Diff line change

@@ -201,6 +201,14 @@ To lint the code, run:

201 201

make lint

202 202

```

203 203 204 +

This includes a custom linter that checks for unescaped commas in `jsonschema` struct tags. The commas in `description` fields must be escaped with `\\,` to prevent silent truncation. You can run just this linter with:

205 + 206 +

```bash

207 +

make lint-jsonschema

208 +

```

209 + 210 +

See the [JSONSchema Linter documentation](internal/linter/jsonschema/README.md) for more details.

211 + 204 212

## License

205 213 206 214

This project is licensed under the [Apache License, Version 2.0](LICENSE).

Original file line number Diff line number Diff line change

@@ -0,0 +1,57 @@

1 +

package main

2 + 3 +

import (

4 +

"flag"

5 +

"fmt"

6 +

"os"

7 +

"path/filepath"

8 + 9 +

linter "github.com/grafana/mcp-grafana/internal/linter/jsonschema"

10 +

)

11 + 12 +

func main() {

13 +

var (

14 +

basePath string

15 +

help bool

16 +

fix bool

17 +

)

18 + 19 +

flag.StringVar(&basePath, "path", ".", "Base directory to scan for Go files")

20 +

flag.BoolVar(&help, "help", false, "Show help message")

21 +

flag.BoolVar(&fix, "fix", false, "Automatically fix unescaped commas")

22 +

flag.Parse()

23 + 24 +

if help {

25 +

fmt.Println("jsonschema-linter - A tool to find unescaped commas in jsonschema struct tags")

26 +

fmt.Println("\nUsage:")

27 +

flag.PrintDefaults()

28 +

os.Exit(0)

29 +

}

30 + 31 +

// Resolve to absolute path

32 +

absPath, err := filepath.Abs(basePath)

33 +

if err != nil {

34 +

fmt.Fprintf(os.Stderr, "Error resolving path: %v\n", err)

35 +

os.Exit(1)

36 +

}

37 + 38 +

// Initialize linter

39 +

jsonLinter := &linter.JSONSchemaLinter{

40 +

FixMode: fix,

41 +

}

42 + 43 +

// Find unescaped commas

44 +

err = jsonLinter.FindUnescapedCommas(absPath)

45 +

if err != nil {

46 +

fmt.Fprintf(os.Stderr, "Error scanning files: %v\n", err)

47 +

os.Exit(1)

48 +

}

49 + 50 +

// Print errors

51 +

jsonLinter.PrintErrors()

52 + 53 +

// Exit with error code if issues were found

54 +

if len(jsonLinter.Errors) > 0 {

55 +

os.Exit(1)

56 +

}

57 +

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,61 @@

1 +

# JSONSchema Linter

2 + 3 +

This linter helps detect and prevent a common issue with Go struct tags in this project.

4 + 5 +

## The Problem

6 + 7 +

In Go struct tags using `jsonschema`, commas in the `description` field need to be escaped using `\\,` syntax. If commas aren't properly escaped, the description is silently truncated at the comma.

8 + 9 +

For example:

10 + 11 +

```go

12 +

// Problematic (description will be truncated at the first comma):

13 +

type Example struct {

14 +

Field string `jsonschema:"description=This is a description, but it will be truncated here"`

15 +

}

16 + 17 +

// Correct (commas properly escaped):

18 +

type Example struct {

19 +

Field string `jsonschema:"description=This is a description\\, and it will be fully included"`

20 +

}

21 +

```

22 + 23 +

## Usage

24 + 25 +

You can use this linter by running:

26 + 27 +

```shell

28 +

make lint-jsonschema

29 +

```

30 + 31 +

or directly:

32 + 33 +

```shell

34 +

go run ./cmd/linters/jsonschema --path .

35 +

```

36 + 37 +

### Auto-fixing issues

38 + 39 +

The linter can automatically fix unescaped commas in jsonschema descriptions by running:

40 + 41 +

```shell

42 +

make lint-jsonschema-fix

43 +

```

44 + 45 +

or directly:

46 + 47 +

```shell

48 +

go run ./cmd/linters/jsonschema --path . --fix

49 +

```

50 + 51 +

This will scan the codebase for unescaped commas and automatically escape them, then report what was fixed.

52 + 53 +

## Flags

54 + 55 +

- `--path`: Base directory to scan for Go files (default: ".")

56 +

- `--fix`: Automatically fix unescaped commas

57 +

- `--help`: Display help information

58 + 59 +

## Integration

60 + 61 +

This linter is integrated into the default `make lint` command, ensuring all PRs are checked for this issue.

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4