go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/git/changetype.go (about)

     1  package git
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  // MarshalJSON returns a string representation of the change.
    10  func (c *Commit_TreeDiff_ChangeType) MarshalJSON() ([]byte, error) {
    11  	return json.Marshal(c.String())
    12  }
    13  
    14  // UnmarshalJSON parses a string representation of the change.
    15  func (c *Commit_TreeDiff_ChangeType) UnmarshalJSON(data []byte) error {
    16  	var s string
    17  	if err := json.Unmarshal(data, &s); err != nil {
    18  		return err
    19  	}
    20  
    21  	change, ok := Commit_TreeDiff_ChangeType_value[strings.ToUpper(s)]
    22  	if !ok {
    23  		return fmt.Errorf("unexpected change type %q", s)
    24  	}
    25  
    26  	*c = Commit_TreeDiff_ChangeType(change)
    27  	return nil
    28  }