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