Skip to content

Commit

Permalink
De-dupe marshalling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieegan3 committed Sep 20, 2023
1 parent 604db03 commit 4e21371
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions ast/location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,20 @@ func (loc *Location) Compare(other *Location) int {

func (loc *Location) MarshalJSON() ([]byte, error) {
// structs are used here to preserve the field ordering of the original Location struct
var data interface{}
if loc.JSONOptions.MarshalOptions.IncludeLocationText {
data = struct {
File string `json:"file"`
Row int `json:"row"`
Col int `json:"col"`
Text []byte `json:"text"`
}{
File: loc.File,
Row: loc.Row,
Col: loc.Col,
Text: loc.Text,
}
return json.Marshal(data)
}

data = struct {
data := struct {
File string `json:"file"`
Row int `json:"row"`
Col int `json:"col"`
Text []byte `json:"text,omitempty"`
}{
File: loc.File,
Row: loc.Row,
Col: loc.Col,
}

if loc.JSONOptions.MarshalOptions.IncludeLocationText {
data.Text = loc.Text
}

return json.Marshal(data)
}

0 comments on commit 4e21371

Please sign in to comment.