github.com/zmap/zlint@v1.1.0/lints/result.go (about)

     1  package lints
     2  
     3  /*
     4   * ZLint Copyright 2017 Regents of the University of Michigan
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
     7   * use this file except in compliance with the License. You may obtain a copy
     8   * of the License at http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    13   * implied. See the License for the specific language governing
    14   * permissions and limitations under the License.
    15   */
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"strings"
    21  )
    22  
    23  // LintStatus is an enum returned by lints inside of a LintResult.
    24  type LintStatus int
    25  
    26  // Known LintStatus values
    27  const (
    28  	// Unused / unset LintStatus
    29  	Reserved LintStatus = 0
    30  
    31  	// Not Applicable
    32  	NA LintStatus = 1
    33  
    34  	// Not Effective
    35  	NE LintStatus = 2
    36  
    37  	Pass   LintStatus = 3
    38  	Notice LintStatus = 4
    39  	Warn   LintStatus = 5
    40  	Error  LintStatus = 6
    41  	Fatal  LintStatus = 7
    42  )
    43  
    44  var (
    45  	// statusLabelToLintStatus is used to work backwards from
    46  	// a LintStatus.String() to the LintStatus. This is used by
    47  	// LintStatus.Unmarshal.
    48  	statusLabelToLintStatus = map[string]LintStatus{
    49  		Reserved.String(): Reserved,
    50  		NA.String():       NA,
    51  		NE.String():       NE,
    52  		Pass.String():     Pass,
    53  		Notice.String():   Notice,
    54  		Warn.String():     Warn,
    55  		Error.String():    Error,
    56  		Fatal.String():    Fatal,
    57  	}
    58  )
    59  
    60  // LintResult contains a LintStatus, and an optional human-readable description.
    61  // The output of a lint is a LintResult.
    62  type LintResult struct {
    63  	Status  LintStatus `json:"result"`
    64  	Details string     `json:"details,omitempty"`
    65  }
    66  
    67  // MarshalJSON implements the json.Marshaler interface.
    68  func (e LintStatus) MarshalJSON() ([]byte, error) {
    69  	s := e.String()
    70  	return json.Marshal(s)
    71  }
    72  
    73  // UnmarshalJSON implements the json.Unmarshaler interface.
    74  func (e *LintStatus) UnmarshalJSON(data []byte) error {
    75  	key := strings.ReplaceAll(string(data), `"`, "")
    76  	if status, ok := statusLabelToLintStatus[key]; ok {
    77  		*e = status
    78  	} else {
    79  		return fmt.Errorf("bad LintStatus JSON value: %s", string(data))
    80  	}
    81  	return nil
    82  }
    83  
    84  // String returns the canonical representation of a LintStatus as a string.
    85  func (e LintStatus) String() string {
    86  	switch e {
    87  	case Reserved:
    88  		return "reserved"
    89  	case NA:
    90  		return "NA"
    91  	case NE:
    92  		return "NE"
    93  	case Pass:
    94  		return "pass"
    95  	case Notice:
    96  		return "info"
    97  	case Warn:
    98  		return "warn"
    99  	case Error:
   100  		return "error"
   101  	case Fatal:
   102  		return "fatal"
   103  	default:
   104  		return ""
   105  	}
   106  }