src/pkg/go/token/serialize.go - The Go Programming Language

Golang

Source file src/pkg/go/token/serialize.go

     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(interface{}) 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{s, f.Name, f.Base, f.Size, f.Lines, f.Infos}
    34		}
    35		s.files = files
    36		s.last = nil
    37		s.mutex.Unlock()
    38	
    39		return nil
    40	}
    41	
    42	// Write calls encode to serialize the file set s.
    43	func (s *FileSet) Write(encode func(interface{}) error) error {
    44		var ss serializedFileSet
    45	
    46		s.mutex.Lock()
    47		ss.Base = s.base
    48		files := make([]serializedFile, len(s.files))
    49		for i, f := range s.files {
    50			files[i] = serializedFile{f.name, f.base, f.size, f.lines, f.infos}
    51		}
    52		ss.Files = files
    53		s.mutex.Unlock()
    54	
    55		return encode(ss)
    56	}