| 1 |
// Copyright 2011 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 token |
| 6 |
|
| 7 |
import "slices" |
| 8 |
|
| 9 |
type serializedFile struct { |
| 10 |
// fields correspond 1:1 to fields with same (lower-case) name in File |
| 11 |
Name string |
| 12 |
Base int |
| 13 |
Size int |
| 14 |
Lines []int |
| 15 |
Infos []lineInfo |
| 16 |
} |
| 17 |
|
| 18 |
type serializedFileSet struct { |
| 19 |
Base int |
| 20 |
Files []serializedFile |
| 21 |
} |
| 22 |
|
| 23 |
// Read calls decode to deserialize a file set into s; s must not be nil. |
| 24 |
func (s *FileSet) Read(decode func(any) error) error { |
| 25 |
var ss serializedFileSet |
| 26 |
if err := decode(&ss); err != nil { |
| 27 |
return err |
| 28 |
} |
| 29 |
|
| 30 |
s.mutex.Lock() |
| 31 |
s.base = ss.Base |
| 32 |
for _, f := range ss.Files { |
| 33 |
s.tree.add(&File{ |
| 34 |
name: f.Name, |
| 35 |
base: f.Base, |
| 36 |
size: f.Size, |
| 37 |
lines: f.Lines, |
| 38 |
infos: f.Infos, |
| 39 |
}) |
| 40 |
} |
| 41 |
s.last.Store(nil) |
| 42 |
s.mutex.Unlock() |
| 43 |
|
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
// Write calls encode to serialize the file set s. |
| 48 |
func (s *FileSet) Write(encode func(any) error) error { |
| 49 |
var ss serializedFileSet |
| 50 |
|
| 51 |
s.mutex.Lock() |
| 52 |
ss.Base = s.base |
| 53 |
var files []serializedFile |
| 54 |
for f := range s.tree.all() { |
| 55 |
f.mutex.Lock() |
| 56 |
files = append(files, serializedFile{ |
| 57 |
Name: f.name, |
| 58 |
Base: f.base, |
| 59 |
Size: f.size, |
| 60 |
Lines: slices.Clone(f.lines), |
| 61 |
Infos: slices.Clone(f.infos), |
| 62 |
}) |
| 63 |
f.mutex.Unlock() |
| 64 |
} |
| 65 |
ss.Files = files |
| 66 |
s.mutex.Unlock() |
| 67 |
|
| 68 |
return encode(ss) |
| 69 |
} |