Skip to content

Commit

Permalink
Use .Value*Pointer() (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive authored Mar 26, 2023
1 parent 715a491 commit a340966
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 97 deletions.
9 changes: 3 additions & 6 deletions internal/provider/dashboard_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -1369,13 +1369,11 @@ func (d *DashboardDataSource) Read(ctx context.Context, req datasource.ReadReque
v.Auto = auto.Enabled.ValueBool()

if !auto.StepCount.IsNull() {
value := auto.StepCount.ValueInt64()
v.AutoCount = &value
v.AutoCount = auto.StepCount.ValueInt64Pointer()
}

if !auto.MinInterval.IsNull() {
value := auto.MinInterval.ValueString()
v.AutoMin = &value
v.AutoMin = auto.MinInterval.ValueStringPointer()
}

if v.Auto {
Expand Down Expand Up @@ -1552,8 +1550,7 @@ func (d *DashboardDataSource) Read(ctx context.Context, req datasource.ReadReque

for _, picker := range timeOptions.TimePicker {
if !picker.Hide.IsNull() {
value := picker.Hide.ValueBool()
dashboard.Timepicker.Hidden = &value
dashboard.Timepicker.Hidden = picker.Hide.ValueBoolPointer()
}

if !picker.NowDelay.IsNull() {
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/gauge_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,11 @@ func (d *GaugeDataSource) Read(ctx context.Context, req datasource.ReadRequest,
}

if !graph.ShowThresholdLabels.IsNull() {
show := graph.ShowThresholdLabels.ValueBool()
options.ShowThresholdLabels = &show
options.ShowThresholdLabels = graph.ShowThresholdLabels.ValueBoolPointer()
}

if !graph.ShowThresholdMarkers.IsNull() {
show := graph.ShowThresholdMarkers.ValueBool()
options.ShowThresholdMarkers = &show
options.ShowThresholdMarkers = graph.ShowThresholdMarkers.ValueBoolPointer()
}

updateTextSize(&options.TextSize, graph.TextSize)
Expand Down
12 changes: 6 additions & 6 deletions internal/provider/grafana/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ type (
Overrides []FieldOverride `json:"overrides,omitempty"`
}
TextSize struct {
TitleSize *int `json:"titleSize,omitempty"`
ValueSize *int `json:"valueSize,omitempty"`
TitleSize *int64 `json:"titleSize,omitempty"`
ValueSize *int64 `json:"valueSize,omitempty"`
}
ReduceOptions struct {
Values bool `json:"values"`
Fields string `json:"fields"`
Limit *int `json:"limit,omitempty"`
Limit *int64 `json:"limit,omitempty"`
Calcs []string `json:"calcs"`
}
Options struct {
Expand Down Expand Up @@ -381,7 +381,7 @@ type (
}
FieldConfigDefaults struct {
Unit string `json:"unit"`
Decimals *int `json:"decimals,omitempty"`
Decimals *int64 `json:"decimals,omitempty"`
Min *float64 `json:"min,omitempty"`
Max *float64 `json:"max,omitempty"`
NoValue *float64 `json:"noValue,omitempty"`
Expand All @@ -398,8 +398,8 @@ type (
FieldConfigCustom struct {
AxisLabel string `json:"axisLabel,omitempty"`
AxisPlacement string `json:"axisPlacement"`
AxisSoftMin *int `json:"axisSoftMin,omitempty"`
AxisSoftMax *int `json:"axisSoftMax,omitempty"`
AxisSoftMin *int64 `json:"axisSoftMin,omitempty"`
AxisSoftMax *int64 `json:"axisSoftMax,omitempty"`
BarAlignment int `json:"barAlignment"`
DrawStyle string `json:"drawStyle"`
FillOpacity int `json:"fillOpacity"`
Expand Down
49 changes: 10 additions & 39 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,11 @@ func (p *GrafanaDashboardBuilderProvider) Configure(ctx context.Context, req pro
}

if !axis.SoftMin.IsNull() {
min := int(axis.SoftMin.ValueInt64())
defaults.Timeseries.Axis.SoftMin = &min
defaults.Timeseries.Axis.SoftMin = axis.SoftMin.ValueInt64Pointer()
}

if !axis.SoftMax.IsNull() {
max := int(axis.SoftMax.ValueInt64())
defaults.Timeseries.Axis.SoftMax = &max
defaults.Timeseries.Axis.SoftMax = axis.SoftMax.ValueInt64Pointer()
}

for _, scale := range axis.Scale {
Expand Down Expand Up @@ -485,25 +483,10 @@ func updateFieldDefaults(defaults *FieldDefaults, opts []FieldOptions) {
defaults.Unit = field.Unit.ValueString()
}

if !field.Decimals.IsNull() {
decimals := int(field.Decimals.ValueInt64())
defaults.Decimals = &decimals
}

if !field.Min.IsNull() {
min := field.Min.ValueFloat64()
defaults.Min = &min
}

if !field.Max.IsNull() {
max := field.Max.ValueFloat64()
defaults.Max = &max
}

if !field.NoValue.IsNull() {
noValue := field.NoValue.ValueFloat64()
defaults.NoValue = &noValue
}
defaults.Decimals = field.Decimals.ValueInt64Pointer()
defaults.Min = field.Min.ValueFloat64Pointer()
defaults.Max = field.Max.ValueFloat64Pointer()
defaults.NoValue = field.NoValue.ValueFloat64Pointer()

for _, color := range field.Color {
if !color.Mode.IsNull() {
Expand All @@ -529,11 +512,7 @@ func updateFieldDefaults(defaults *FieldDefaults, opts []FieldOptions) {
for i, step := range threshold.Steps {
s := ThresholdStepDefaults{
Color: step.Color.ValueString(),
}

if !step.Value.IsNull() {
value := step.Value.ValueFloat64()
s.Value = &value
Value: step.Value.ValueFloat64Pointer(),
}

steps[i] = s
Expand All @@ -546,15 +525,8 @@ func updateFieldDefaults(defaults *FieldDefaults, opts []FieldOptions) {

func updateTextSizeDefaults(defaults *TextSizeDefaults, opts []TextSizeOptions) {
for _, textSize := range opts {
if !textSize.Title.IsNull() {
size := int(textSize.Title.ValueInt64())
defaults.Title = &size
}

if !textSize.Value.IsNull() {
size := int(textSize.Value.ValueInt64())
defaults.Value = &size
}
defaults.Title = textSize.Title.ValueInt64Pointer()
defaults.Value = textSize.Value.ValueInt64Pointer()
}
}

Expand All @@ -569,8 +541,7 @@ func updateReduceOptionsDefaults(defaults *ReduceOptionDefaults, opts []ReduceOp
}

if !reducer.Limit.IsNull() {
limit := int(reducer.Limit.ValueInt64())
defaults.Limit = &limit
defaults.Limit = reducer.Limit.ValueInt64Pointer()
}

if !reducer.Calculation.IsNull() {
Expand Down
6 changes: 2 additions & 4 deletions internal/provider/timeseries_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,11 @@ func (d *TimeseriesDataSource) Read(ctx context.Context, req datasource.ReadRequ
}

if !axis.SoftMin.IsNull() {
min := int(axis.SoftMin.ValueInt64())
fieldConfig.Custom.AxisSoftMin = &min
fieldConfig.Custom.AxisSoftMin = axis.SoftMin.ValueInt64Pointer()
}

if !axis.SoftMax.IsNull() {
max := int(axis.SoftMax.ValueInt64())
fieldConfig.Custom.AxisSoftMax = &max
fieldConfig.Custom.AxisSoftMax = axis.SoftMax.ValueInt64Pointer()
}

for _, scale := range axis.Scale {
Expand Down
52 changes: 14 additions & 38 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func CalculationTypesMarkdown() string {

type FieldDefaults struct {
Unit string
Decimals *int
Decimals *int64
Min *float64
Max *float64
NoValue *float64
Expand Down Expand Up @@ -113,7 +113,7 @@ type ThresholdStepDefaults struct {
type ReduceOptionDefaults struct {
Values bool
Fields string
Limit *int
Limit *int64
Calculation string
}

Expand All @@ -126,15 +126,15 @@ func NewReduceOptionDefaults() ReduceOptionDefaults {
}

type TextSizeDefaults struct {
Title *int
Value *int
Title *int64
Value *int64
}

type AxisDefaults struct {
Label string
Placement string
SoftMin *int
SoftMax *int
SoftMin *int64
SoftMax *int64
Scale ScaleDefaults
}

Expand Down Expand Up @@ -995,25 +995,10 @@ func createFieldConfig(defaults FieldDefaults, fieldOptions []FieldOptions) graf
fieldConfig.Unit = field.Unit.ValueString()
}

if !field.Decimals.IsNull() {
decimals := int(field.Decimals.ValueInt64())
fieldConfig.Decimals = &decimals
}

if !field.Min.IsNull() {
min := field.Min.ValueFloat64()
fieldConfig.Min = &min
}

if !field.Max.IsNull() {
max := field.Max.ValueFloat64()
fieldConfig.Max = &max
}

if !field.NoValue.IsNull() {
noValue := field.NoValue.ValueFloat64()
fieldConfig.NoValue = &noValue
}
fieldConfig.Decimals = field.Decimals.ValueInt64Pointer()
fieldConfig.Min = field.Min.ValueFloat64Pointer()
fieldConfig.Max = field.Max.ValueFloat64Pointer()
fieldConfig.NoValue = field.NoValue.ValueFloat64Pointer()

for _, color := range field.Color {
if !color.Mode.IsNull() {
Expand Down Expand Up @@ -1273,8 +1258,7 @@ func updateThresholds(thresholds *grafana.Thresholds, thresholdOptions []Thresho
}

if !step.Value.IsNull() {
value := step.Value.ValueFloat64()
s.Value = &value
s.Value = step.Value.ValueFloat64Pointer()
}

steps[i] = s
Expand All @@ -1288,15 +1272,8 @@ func updateThresholds(thresholds *grafana.Thresholds, thresholdOptions []Thresho

func updateTextSize(options *grafana.TextSize, opts []TextSizeOptions) {
for _, textSize := range opts {
if !textSize.Title.IsNull() {
size := int(textSize.Title.ValueInt64())
options.TitleSize = &size
}

if !textSize.Value.IsNull() {
size := int(textSize.Value.ValueInt64())
options.ValueSize = &size
}
options.TitleSize = textSize.Title.ValueInt64Pointer()
options.ValueSize = textSize.Value.ValueInt64Pointer()
}
}

Expand All @@ -1311,8 +1288,7 @@ func updateReduceOptions(options *grafana.ReduceOptions, opts []ReduceOptions) {
}

if !reducer.Limit.IsNull() {
limit := int(reducer.Limit.ValueInt64())
options.Limit = &limit
options.Limit = reducer.Limit.ValueInt64Pointer()
}

if !reducer.Calculation.IsNull() {
Expand Down

0 comments on commit a340966

Please sign in to comment.