github.com/TBD54566975/ftl@v0.219.0/internal/model/model.go (about)

     1  package model
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/TBD54566975/ftl/backend/schema"
    10  	"github.com/TBD54566975/ftl/internal/sha256"
    11  )
    12  
    13  type Deployment struct {
    14  	Module    string
    15  	Language  string
    16  	Key       DeploymentKey
    17  	Schema    *schema.Module
    18  	Artefacts []*Artefact
    19  }
    20  
    21  // Close is a convenience function to close all artefacts.
    22  func (d *Deployment) Close() error {
    23  	errs := make([]error, 0, len(d.Artefacts))
    24  	for _, a := range d.Artefacts {
    25  		errs = append(errs, a.Content.Close())
    26  	}
    27  	return errors.Join(errs...)
    28  }
    29  
    30  type Artefact struct {
    31  	Path       string
    32  	Executable bool
    33  	Digest     sha256.SHA256
    34  	// ~Zero-cost on-demand reader.
    35  	Content io.ReadCloser
    36  }
    37  
    38  type Labels map[string]any
    39  
    40  func (l Labels) String() string {
    41  	w := strings.Builder{}
    42  	i := 0
    43  	for k, v := range l {
    44  		if i > 0 {
    45  			w.WriteString(" ")
    46  		}
    47  		i++
    48  		w.WriteString(k)
    49  		w.WriteString("=")
    50  		writeValue(&w, v)
    51  	}
    52  	return w.String()
    53  }
    54  
    55  func writeValue(w *strings.Builder, v any) {
    56  	switch v := v.(type) {
    57  	case string:
    58  		w.WriteString(v)
    59  	case float64:
    60  		w.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
    61  	case int:
    62  		w.WriteString(strconv.Itoa(v))
    63  	case bool:
    64  		w.WriteString(strconv.FormatBool(v))
    65  	case []any:
    66  		for i, v := range v {
    67  			if i > 0 {
    68  				w.WriteString(",")
    69  			}
    70  			writeValue(w, v)
    71  		}
    72  	default:
    73  		panic("unknown label type")
    74  	}
    75  }