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