Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

68 lignes
1.4KB

  1. package pattern
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v3"
  5. "io/fs"
  6. "math"
  7. "gopkg.in/Knetic/govaluate.v3"
  8. )
  9. // DimensionID describe the ID of the dimension.
  10. type DimensionID string
  11. // Dimensions is a map with dimensions.
  12. type Dimensions map[DimensionID]Dimension
  13. // Parameters returns a govaluate.MapParameters object based on the dimensions.
  14. func (d Dimensions) Parameters() govaluate.MapParameters {
  15. parameters := govaluate.MapParameters{}
  16. parameters["pi"] = math.Pi
  17. for id, dimension := range d {
  18. parameters[string(id)] = dimension.Value
  19. }
  20. return parameters
  21. }
  22. func (d Dimensions) Names(templateDir fs.FS) (map[string]string, error) {
  23. f, err := templateDir.Open("dimension_names.yaml")
  24. if err != nil {
  25. return nil, fmt.Errorf("open \"dimension_names.yaml\": %w", err)
  26. }
  27. namedDimensions := Dimensions{}
  28. err = yaml.NewDecoder(f).Decode(&namedDimensions)
  29. if err != nil {
  30. return nil, fmt.Errorf("decode yaml from \"dimension_names.yaml\": %w", err)
  31. }
  32. out := make(map[string]string)
  33. for id, dimension := range d {
  34. nd, ok := namedDimensions[id]
  35. if !ok {
  36. continue
  37. }
  38. value := math.Round(dimension.Value) / 10
  39. out[nd.Name] = fmt.Sprintf("%.1f mm", value)
  40. }
  41. return out, nil
  42. }
  43. // Dimension is a combination of a name and a value.
  44. type Dimension struct {
  45. Name string
  46. Value float64
  47. }
  48. // AddDimension adds a dimension to a pattern.
  49. func (p *Pattern) AddDimension(id DimensionID, dimension Dimension) {
  50. p.dimensions[id] = dimension
  51. }