Skip to content

Commit

Permalink
Marshal GitHub connector config with JSON annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mogottsch committed Sep 12, 2023
1 parent e297164 commit b7d399e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/idp/provider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand All @@ -11,14 +12,14 @@ import (
"github.com/giantswarm/dex-operator/pkg/idp/provider"
"github.com/giantswarm/dex-operator/pkg/idp/provider/github/manifest"
"github.com/giantswarm/dex-operator/pkg/key"
"github.com/giantswarm/dex-operator/pkg/yaml"

"github.com/bradleyfalzon/ghinstallation/v2"
githubconnector "github.com/dexidp/dex/connector/github"
"github.com/giantswarm/microerror"
"github.com/go-logr/logr"
githubclient "github.com/google/go-github/v50/github"
"github.com/skratchdot/open-golang/open"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -178,7 +179,7 @@ func (g *Github) CreateOrUpdateApp(config provider.AppConfig, ctx context.Contex
RedirectURI: config.RedirectURI,
TeamNameField: TeamNameFieldSlug,
}
data, err := yaml.Marshal(connectorConfig)
data, err := yaml.MarshalWithJsonAnnotations(connectorConfig)
if err != nil {
return provider.ProviderApp{}, microerror.Mask(err)
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package yaml

import (
"encoding/json"

"github.com/giantswarm/microerror"
yamllib "gopkg.in/yaml.v2"
)

// MarshalWithJsonAnnotations marshals the given value to YAML, but uses the
// JSON annotations of the struct fields.
func MarshalWithJsonAnnotations(v any) ([]byte, error) {
jsonData, jsonErr := json.Marshal(v)
if jsonErr != nil {
return nil, microerror.Mask(jsonErr)
}
var mapData map[string]interface{}
jsonErr = json.Unmarshal(jsonData, &mapData)
if jsonErr != nil {
return nil, microerror.Mask(jsonErr)
}
data, yamlErr := yamllib.Marshal(mapData)
return data, yamlErr
}

0 comments on commit b7d399e

Please sign in to comment.