| 
									
										
										
										
											2017-08-29 10:47:29 -07:00
										 |  |  | package formatter | 
					
						
							| 
									
										
										
										
											2017-08-27 16:57:16 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 15:44:03 -08:00
										 |  |  | 	"github.com/mgechev/revive/lint" | 
					
						
							| 
									
										
										
										
											2017-08-27 16:57:16 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:01:18 -08:00
										 |  |  | // JSON is an implementation of the Formatter interface | 
					
						
							| 
									
										
										
										
											2017-08-27 16:57:16 -07:00
										 |  |  | // which formats the errors to JSON. | 
					
						
							| 
									
										
										
										
											2018-01-27 17:01:18 -08:00
										 |  |  | type JSON struct { | 
					
						
							| 
									
										
										
										
											2018-01-24 15:44:03 -08:00
										 |  |  | 	Metadata lint.FormatterMetadata | 
					
						
							| 
									
										
										
										
											2017-08-27 16:57:16 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 16:22:17 -08:00
										 |  |  | // Name returns the name of the formatter | 
					
						
							| 
									
										
										
										
											2022-04-10 11:55:13 +02:00
										 |  |  | func (*JSON) Name() string { | 
					
						
							| 
									
										
										
										
											2018-01-27 16:22:17 -08:00
										 |  |  | 	return "json" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 05:36:45 +02:00
										 |  |  | // jsonObject defines a JSON object of an failure | 
					
						
							|  |  |  | type jsonObject struct { | 
					
						
							|  |  |  | 	Severity     lint.Severity | 
					
						
							|  |  |  | 	lint.Failure `json:",inline"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 15:44:03 -08:00
										 |  |  | // Format formats the failures gotten from the lint. | 
					
						
							| 
									
										
										
										
											2022-04-10 11:55:13 +02:00
										 |  |  | func (*JSON) Format(failures <-chan lint.Failure, config lint.Config) (string, error) { | 
					
						
							| 
									
										
										
										
											2018-07-13 05:36:45 +02:00
										 |  |  | 	var slice []jsonObject | 
					
						
							| 
									
										
										
										
											2018-01-23 18:19:06 -08:00
										 |  |  | 	for failure := range failures { | 
					
						
							| 
									
										
										
										
											2018-07-13 05:36:45 +02:00
										 |  |  | 		obj := jsonObject{} | 
					
						
							|  |  |  | 		obj.Severity = severity(config, failure) | 
					
						
							|  |  |  | 		obj.Failure = failure | 
					
						
							|  |  |  | 		slice = append(slice, obj) | 
					
						
							| 
									
										
										
										
											2017-08-27 16:57:16 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-23 18:19:06 -08:00
										 |  |  | 	result, err := json.Marshal(slice) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return string(result), err | 
					
						
							| 
									
										
										
										
											2017-08-27 16:57:16 -07:00
										 |  |  | } |