github.com/mistwind/reviewdog@v0.0.0-20230322024206-9cfa11856d58/parser/rdjson_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"google.golang.org/protobuf/encoding/protojson"
    10  )
    11  
    12  func ExampleRDJSONParser() {
    13  	const sample = `
    14  {
    15    "source": {
    16      "name": "linter-name",
    17      "url": "https://github.com/reviewdog#linter-name"
    18    },
    19    "severity": "INFO",
    20    "diagnostics": [
    21      {
    22        "source": {
    23          "name": "deadcode"
    24        },
    25        "message": "'unused' is unused",
    26        "location": {
    27          "path": "testdata/main.go",
    28          "range": {
    29            "start": {
    30              "line": 18,
    31              "column": 6
    32            }
    33          }
    34        }
    35      },
    36      {
    37        "message": "printf: Sprintf format %d reads arg #1, but call has 0 args",
    38        "location": {
    39          "path": "testdata/main.go",
    40          "range": {
    41            "start": {
    42              "line": 13,
    43              "column": 2
    44            }
    45          }
    46        }
    47      },
    48      {
    49        "source": {
    50          "name": "severity-test"
    51        },
    52        "message": "severity test (string)",
    53        "location": {
    54          "path": "testdata/main.go",
    55          "range": {
    56            "start": {
    57              "line": 24,
    58              "column": 6
    59            }
    60          }
    61        },
    62        "severity": "WARNING"
    63      },
    64      {
    65        "source": {
    66          "name": "severity-test"
    67        },
    68        "message": "severity test (number)",
    69        "location": {
    70          "path": "testdata/main.go",
    71          "range": {
    72            "start": {
    73              "line": 24,
    74              "column": 6
    75            }
    76          }
    77        },
    78        "severity": 1
    79      }
    80    ]
    81  }`
    82  	p := NewRDJSONParser()
    83  	diagnostics, err := p.Parse(strings.NewReader(sample))
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  	for _, d := range diagnostics {
    88  		d.OriginalOutput = "" // Skip for testing as it's not deterministic.
    89  		rdjson, _ := protojson.MarshalOptions{Indent: "  "}.Marshal(d)
    90  		var out bytes.Buffer
    91  		json.Indent(&out, rdjson, "", "  ")
    92  		fmt.Println(out.String())
    93  	}
    94  	// Output:
    95  	// {
    96  	//   "message": "'unused' is unused",
    97  	//   "location": {
    98  	//     "path": "testdata/main.go",
    99  	//     "range": {
   100  	//       "start": {
   101  	//         "line": 18,
   102  	//         "column": 6
   103  	//       }
   104  	//     }
   105  	//   },
   106  	//   "severity": "INFO",
   107  	//   "source": {
   108  	//     "name": "deadcode"
   109  	//   }
   110  	// }
   111  	// {
   112  	//   "message": "printf: Sprintf format %d reads arg #1, but call has 0 args",
   113  	//   "location": {
   114  	//     "path": "testdata/main.go",
   115  	//     "range": {
   116  	//       "start": {
   117  	//         "line": 13,
   118  	//         "column": 2
   119  	//       }
   120  	//     }
   121  	//   },
   122  	//   "severity": "INFO",
   123  	//   "source": {
   124  	//     "name": "linter-name",
   125  	//     "url": "https://github.com/reviewdog#linter-name"
   126  	//   }
   127  	// }
   128  	// {
   129  	//   "message": "severity test (string)",
   130  	//   "location": {
   131  	//     "path": "testdata/main.go",
   132  	//     "range": {
   133  	//       "start": {
   134  	//         "line": 24,
   135  	//         "column": 6
   136  	//       }
   137  	//     }
   138  	//   },
   139  	//   "severity": "WARNING",
   140  	//   "source": {
   141  	//     "name": "severity-test"
   142  	//   }
   143  	// }
   144  	// {
   145  	//   "message": "severity test (number)",
   146  	//   "location": {
   147  	//     "path": "testdata/main.go",
   148  	//     "range": {
   149  	//       "start": {
   150  	//         "line": 24,
   151  	//         "column": 6
   152  	//       }
   153  	//     }
   154  	//   },
   155  	//   "severity": "ERROR",
   156  	//   "source": {
   157  	//     "name": "severity-test"
   158  	//   }
   159  	// }
   160  }