golang.org/x/tools/gopls@v0.15.3/internal/protocol/tsdocument_changes.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package protocol
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  )
    11  
    12  // DocumentChanges is a union of a file edit and directory rename operations
    13  // for package renaming feature. At most one field of this struct is non-nil.
    14  type DocumentChanges struct {
    15  	TextDocumentEdit *TextDocumentEdit
    16  	RenameFile       *RenameFile
    17  }
    18  
    19  func (d *DocumentChanges) UnmarshalJSON(data []byte) error {
    20  	var m map[string]interface{}
    21  
    22  	if err := json.Unmarshal(data, &m); err != nil {
    23  		return err
    24  	}
    25  
    26  	if _, ok := m["textDocument"]; ok {
    27  		d.TextDocumentEdit = new(TextDocumentEdit)
    28  		return json.Unmarshal(data, d.TextDocumentEdit)
    29  	}
    30  
    31  	d.RenameFile = new(RenameFile)
    32  	return json.Unmarshal(data, d.RenameFile)
    33  }
    34  
    35  func (d *DocumentChanges) MarshalJSON() ([]byte, error) {
    36  	if d.TextDocumentEdit != nil {
    37  		return json.Marshal(d.TextDocumentEdit)
    38  	} else if d.RenameFile != nil {
    39  		return json.Marshal(d.RenameFile)
    40  	}
    41  	return nil, fmt.Errorf("Empty DocumentChanges union value")
    42  }