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

Unmarshal Source Data to Struct #27

Merged
merged 3 commits into from
Feb 27, 2022
Merged

Unmarshal Source Data to Struct #27

merged 3 commits into from
Feb 27, 2022

Conversation

Piszmog
Copy link
Owner

@Piszmog Piszmog commented Feb 27, 2022

Add functionality to convert the slice of PropertySource in Source to a struct.

Example

type ConfigStruct struct {
	StringVal   string         `json:"stringVal"`
	IntVal      int            `json:"intVal"`
	SliceString []string       `json:"sliceString"`
	SliceInt    []int          `json:"sliceInt"`
	SliceStruct []NestedConfig `json:"sliceStruct"`
}

type NestedConfig struct {
	StringVal string `json:"stringVal"`
	IntVal    int    `json:"intVal"`
}

func main() {
        // say we have the following source data (usually get it from calling GetConfiguration)
	source := cloudconfigclient.Source{
		PropertySources: []cloudconfigclient.PropertySource{
			{
				Name: "application-foo.yml",
				Source: map[string]interface{}{
					"stringVal":                "value1",
					"intVal":                   1,
					"sliceString[0]":           "value2",
					"sliceInt[0]":              2,
					"sliceStruct[0].stringVal": "value5",
					"sliceStruct[0].intVal":    4,
					"sliceStruct[1].intVal":    5,
					"sliceStruct[1].stringVal": "value6",
				},
			},
		},
	}

        // unmarshal to the struct
	var config ConfigStruct
	if err := source.Unmarshal(&config); err != nil {
		fmt.Println(err)
		return
	}
	// do something with the struct now
}

@sonarcloud
Copy link

sonarcloud bot commented Feb 27, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

@coveralls
Copy link

Pull Request Test Coverage Report for Build 1907398805

  • 52 of 65 (80.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-4.3%) to 95.667%

Changes Missing Coverage Covered Lines Changed/Added Lines %
configuration.go 52 65 80.0%
Totals Coverage Status
Change from base Build 1416020590: -4.3%
Covered Lines: 287
Relevant Lines: 300

💛 - Coveralls

@Piszmog Piszmog merged commit b1a4d0f into main Feb 27, 2022
@Piszmog Piszmog deleted the unmarshal-properties branch February 27, 2022 22:12
@Piszmog Piszmog mentioned this pull request Feb 27, 2022
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

Successfully merging this pull request may close these issues.

2 participants