github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/api/models/synchronization/entry.go (about)

     1  package synchronization
     2  
     3  import (
     4  	"encoding/hex"
     5  
     6  	"github.com/mutagen-io/mutagen/pkg/synchronization/core"
     7  )
     8  
     9  // Entry represents a filesystem entry.
    10  type Entry struct {
    11  	// Kind encodes the type of filesystem entry being represented.
    12  	Kind core.EntryKind `json:"kind"`
    13  	// DirectoryEntry stores fields that are only relevant to directory entries.
    14  	// It is only non-nil if the entry is a directory.
    15  	*DirectoryEntry
    16  	// FileEntry stores fields that are only relevant to file entries. It is
    17  	// only non-nil if the entry is a file.
    18  	*FileEntry
    19  	// SymbolicLinkEntry stores fields that are only relevant to symbolic link
    20  	// entries. It is only non-nil if the entry is a symbolic link.
    21  	*SymbolicLinkEntry
    22  	// ProblematicEntry stores fields that are only relevant to problematic
    23  	// entries. It is only non-nil if the entry is problematic content.
    24  	*ProblematicEntry
    25  }
    26  
    27  // DirectoryEntry encodes fields relevant to directory entries.
    28  type DirectoryEntry struct {
    29  	// Contents represents a directory entry's contents.
    30  	Contents map[string]*Entry `json:"contents"`
    31  }
    32  
    33  // FileEntry encodes fields relevant to file entries.
    34  type FileEntry struct {
    35  	// Digest represents the hash of a file entry's contents.
    36  	Digest string `json:"digest"`
    37  	// Executable indicates whether or not a file entry is marked as executable.
    38  	Executable bool `json:"executable,omitempty"`
    39  }
    40  
    41  // SymbolicLinkEntry encodes fields relevant to symbolic link entries.
    42  type SymbolicLinkEntry struct {
    43  	// Target is the symbolic link target.
    44  	Target string `json:"target"`
    45  }
    46  
    47  // ProblematicEntry encodes fields relevant to problematic entries.
    48  type ProblematicEntry struct {
    49  	// Problem indicates the relevant error for problematic content.
    50  	Problem string `json:"problem"`
    51  }
    52  
    53  // newEntryFromInternalEntry creates a new entry representation from an internal
    54  // Protocol Buffers representation. The entry must be valid.
    55  func newEntryFromInternalEntry(entry *core.Entry) *Entry {
    56  	// Handle the case of non-existent entries.
    57  	if entry == nil {
    58  		return nil
    59  	}
    60  
    61  	// Create the result.
    62  	result := &Entry{Kind: entry.Kind}
    63  
    64  	// Propagate the relevant fields.
    65  	switch entry.Kind {
    66  	case core.EntryKind_Directory:
    67  		result.DirectoryEntry = &DirectoryEntry{}
    68  		if l := len(entry.Contents); l > 0 {
    69  			result.Contents = make(map[string]*Entry, l)
    70  			for n, c := range entry.Contents {
    71  				result.Contents[n] = newEntryFromInternalEntry(c)
    72  			}
    73  		}
    74  	case core.EntryKind_File:
    75  		result.FileEntry = &FileEntry{
    76  			Digest:     hex.EncodeToString(entry.Digest),
    77  			Executable: entry.Executable,
    78  		}
    79  	case core.EntryKind_SymbolicLink:
    80  		result.SymbolicLinkEntry = &SymbolicLinkEntry{Target: entry.Target}
    81  	case core.EntryKind_Untracked:
    82  		// There are no fields to propagate for untracked content.
    83  	case core.EntryKind_Problematic:
    84  		result.ProblematicEntry = &ProblematicEntry{Problem: entry.Problem}
    85  	default:
    86  		panic("invalid entry kind")
    87  	}
    88  
    89  	// Done.
    90  	return result
    91  }