github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/core/rawdata/utils.go (about)

     1  package rawdata
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strings"
     7  
     8  	"github.com/lib/pq"
     9  )
    10  
    11  const has = true
    12  
    13  type Set map[int64]bool // on choisit bool pour l'interaction avec .js
    14  
    15  func NewSet() Set {
    16  	return map[int64]bool{}
    17  }
    18  
    19  func NewSetFromSlice(keys []int64) Set {
    20  	out := make(Set, len(keys))
    21  	for _, key := range keys {
    22  		out[key] = has
    23  	}
    24  	return out
    25  }
    26  
    27  func (s Set) Keys() []int64 {
    28  	out := make([]int64, 0, len(s))
    29  	for k := range s {
    30  		out = append(out, k)
    31  	}
    32  	return out
    33  }
    34  
    35  func (s Set) Has(key int64) bool {
    36  	_, has := s[key]
    37  	return has
    38  }
    39  
    40  func (s Set) Add(key int64) {
    41  	s[key] = has
    42  }
    43  
    44  type Ids []int64
    45  
    46  func (ids Ids) AsSQL() pq.Int64Array {
    47  	return pq.Int64Array(ids)
    48  }
    49  
    50  func (ids Ids) AsSet() Set {
    51  	return NewSetFromSlice(ids)
    52  }
    53  
    54  // Html renvoie le html d'une ligne de tableau (incluant <tr>)
    55  func Html(r Item, headers []Header) string {
    56  	templateRow := `<td bgcolor="%s"> %s %s %s </td>`
    57  	fields := make([]string, len(headers))
    58  	for index, field := range headers {
    59  		color := ""
    60  		if colorV := r.TextColor(field.Field); colorV != nil {
    61  			color = colorV.Hex()
    62  		}
    63  		boldIn, boldOut := "", ""
    64  		if r.Bolds[field.Field] {
    65  			boldIn, boldOut = "<b>", "</b>"
    66  		}
    67  		fields[index] = fmt.Sprintf(templateRow, color, boldIn, r.Fields.Data(field.Field).String(), boldOut)
    68  	}
    69  	return "<tr>" + strings.Join(fields, "") + "</tr>"
    70  }
    71  
    72  var (
    73  	letterRunes  = []rune("azertyuiopqsdfghjklmwxcvbn123456789")
    74  	specialRunes = []rune("é@!?&èïab ")
    75  )
    76  
    77  func RandString(n int, specialChars bool) string {
    78  	b := make([]rune, n)
    79  	props, maxLength := letterRunes, len(letterRunes)
    80  	if specialChars {
    81  		props = append(props, specialRunes...)
    82  		maxLength += len(specialRunes)
    83  	}
    84  	for i := range b {
    85  		b[i] = props[rand.Intn(maxLength)]
    86  	}
    87  	return string(b)
    88  }
    89  
    90  type StringSet map[string]bool
    91  
    92  func (ss StringSet) ToList() []string {
    93  	out := make([]string, 0, len(ss))
    94  	for s := range ss {
    95  		out = append(out, s)
    96  	}
    97  	return out
    98  }
    99  
   100  // --------------------------------------------
   101  // ---------------- Colors --------------------
   102  // --------------------------------------------
   103  
   104  type Color interface {
   105  	Hex() string
   106  	AHex() string
   107  }
   108  
   109  type RGBA struct {
   110  	R, G, B, A uint8
   111  }
   112  
   113  func (c RGBA) Hex() string {
   114  	return fmt.Sprintf("#%02x%02x%02x", c.R, c.G, c.B)
   115  }
   116  
   117  func (c RGBA) AHex() string {
   118  	if c.A == 0 {
   119  		return ""
   120  	}
   121  	return fmt.Sprintf("#%02x%02x%02x%02x", c.A, c.R, c.G, c.B)
   122  }
   123  
   124  type HexColor string
   125  
   126  func (c HexColor) Hex() string {
   127  	if len(c) == 9 {
   128  		return "#" + string(c[3:])
   129  	}
   130  	return string(c)
   131  }
   132  
   133  func (c HexColor) AHex() string {
   134  	if len(c) == 7 {
   135  		return "#ff" + string(c[1:])
   136  	}
   137  	return string(c)
   138  }
   139  
   140  // remplace une couleur vide (`nil`) par du noir
   141  func defaultC(c Color) Color {
   142  	if c == nil {
   143  		return HexColor("")
   144  	}
   145  	return c
   146  }