Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting partial struct #2

Closed
mrodriguezio opened this issue Feb 21, 2019 · 1 comment
Closed

Getting partial struct #2

mrodriguezio opened this issue Feb 21, 2019 · 1 comment

Comments

@mrodriguezio
Copy link

I'm trying to get this work, but getting unexpected results.

package main

import (
	"encoding/json"
	"fmt"
	"log"

	dynamicstruct "github.com/Ompluscator/dynamic-struct"
)

func main() {
	i := dynamicstruct.NewStruct().
		AddField("AlarmName", "", `json:"alarm_name"`).
		AddField("ComparisonOperator", "", `json:"comparison_operator"`).
		AddField("EvaluationPeriods", 0, `json:"evaluation_periods"`).
		AddField("Threshold", 0, `json:"threshold"`).

		// Optional
		AddField("MetricName", "", `json:"metric_name,omitempty"`).
		AddField("Namespace", "", `json:"namespace,omitempty"`).
		AddField("Period", 0, `json:"period,omitempty"`).
		AddField("Statistic", "", `json:"statistic,omitempty"`).
		AddField("TreatMissingData", "", `json:"treat_missing_data,omitempty"`).
		Build().New()

	data := []byte(`
{
	"ActionsEnabled": null,
	"AlarmActions": ["${aws_autoscaling_policy.jenkins_cluster_scale_up_policy.id}"],
	"AlarmDescription":"CPU utilization above 5% during the last minute",
	"AlarmName":"JenkinsClusterScaleUpAlarm",
	"ComparisonOperator":"GreaterThanOrEqualToThreshold",
	"DatapointsToAlarm":null,
	"Dimensions":[{"Name":"ClusterName","Value":"jenkins-cluster"}],
	"EvaluateLowSampleCountPercentile":null,
	"EvaluationPeriods":1,
	"ExtendedStatistic":null,
	"InsufficientDataActions":null,
	"MetricName":"CPUUtilization",
	"Metrics":null,
	"Namespace":"AWS/ECS",
	"OKActions":null,
	"Period":null,
	"Statistic":"Average",
	"Threshold":20,
	"TreatMissingData":"notBreaching",
	"Unit":null
}
	`)
	var err error
	err = json.Unmarshal(data, &i)
	if err != nil {
		log.Fatal(err)
	}

	data, err = json.Marshal(i)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(data))
}

Actual output:

{"namespace":"AWS/ECS","statistic":"Average","alarm_name":"","comparison_operator":"","evaluation_periods":0,"threshold":20}

Expected output:

{"namespace":"AWS/ECS","statistic":"Average","alarm_name":"JenkinsClusterScaleUpAlarm","comparison_operator":"GreaterThanOrEqualToThreshold","evaluation_periods": 1,"threshold":20,"treat_missing_data":"notBreaching","metric_name":"CPUUtilization"}

Any advice is highly appreciated 😅!

@mrodriguezio mrodriguezio changed the title Getting partial conversion Getting partial struct Feb 21, 2019
@Ompluscator
Copy link
Owner

As you defined your json tags:

AddField("MetricName", "", `json:"metric_name,omitempty"`).
AddField("TreatMissingData", "", `json:"treat_missing_data,omitempty"`).

This means initial json input should contains keys "metric_name,omitempty" and "treat_missing_data,omitempty" and not:

"MetricName":"CPUUtilization",
"TreatMissingData":"notBreaching",

So, json package use tags for both read and write.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants