github.com/netdata/go.d.plugin@v0.58.1/pkg/stm/README.md (about)

     1  <!--
     2  title: "stm"
     3  custom_edit_url: "https://github.com/netdata/go.d.plugin/edit/master/pkg/stm/README.md"
     4  sidebar_label: "stm"
     5  learn_status: "Published"
     6  learn_rel_path: "Developers/External plugins/go.d.plugin/Helper Packages"
     7  -->
     8  
     9  # stm
    10  
    11  This package helps you to convert a struct to `map[string]int64`.
    12  
    13  ## Tags
    14  
    15  The encoding of each struct field can be customized by the format string stored under the `stm` key in the struct
    16  field's tag. The format string gives the name of the field, possibly followed by a comma-separated list of options.
    17  
    18  **Lack of tag means no conversion performed.**
    19  If you don't want a field to be added to the `map[string]int64` just don't add a tag to it.
    20  
    21  Tag syntax:
    22  
    23  ```
    24  `stm:"name,multiplier,divisor"`
    25  ```
    26  
    27  Both `multiplier` and `divisor` are optional, `name` is mandatory.
    28  
    29  Examples of struct field tags and their meanings:
    30  
    31  ```
    32  // Field appears in map as key "name".
    33  Field int `stm:"name"`
    34  
    35  // Field appears in map as key "name" and its value is multiplied by 10.
    36  Field int `stm:"name,10"`
    37  
    38  // Field appears in map as key "name" and its value is multiplied by 10 and divided by 5.
    39  Field int `stm:"name,10,5"`
    40  ```
    41  
    42  ## Supported field value kinds
    43  
    44  The list is:
    45  
    46  - `int`
    47  - `float`
    48  - `bool`
    49  - `map`
    50  - `array`
    51  - `slice`
    52  - `pointer`
    53  - `struct`
    54  - `interface { WriteTo(rv map[string]int64, key string, mul, div int) }`
    55  
    56  It is ok to have nested structures.
    57  
    58  ## Usage
    59  
    60  Use `ToMap` function. Keep in mind:
    61  
    62  - this function is variadic (can be called with any number of trailing arguments).
    63  - it doesn't allow to have duplicate in result map.
    64  - if there is a duplicate key it panics.
    65  
    66  ```
    67  	ms := struct {
    68  		MetricA   int64            `stm:"metric_a"`
    69  		MetricB   float64          `stm:"metric_b,1000"`
    70  		MetricSet map[string]int64 `stm:"metric_set"`
    71  	}{
    72  		MetricA: 10,
    73  		MetricB: 5.5,
    74  		MetricSet: map[string]int64{
    75  			"a": 10,
    76  			"b": 10,
    77  		},
    78  	}
    79  	fmt.Println(stm.ToMap(ms)) // => map[metric_a:10 metric_b:5500 metric_set_a:10 metric_set_b:10]
    80  ```