Skip to content

Commit

Permalink
Add data
Browse files Browse the repository at this point in the history
  • Loading branch information
rucciva committed Jun 19, 2024
1 parent 8498cb3 commit 51471b8
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ require (
github.com/buger/jsonparser v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/invopop/yaml v0.2.0 // indirect
Expand All @@ -25,6 +27,7 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twpayne/go-jsonstruct/v3 v3.0.0 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dprotaso/go-yit v0.0.0-20191028211022-135eb7262960/go.mod h1:9HQzr9D/0PGwMEbC3d5AB7oi67+h4TsQqItC1GVYG58=
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 h1:PRxIJD8XjimM5aTknUK9w6DHLDox2r2M3DI4i2pnd3w=
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936/go.mod h1:ttYvX5qlB+mlV1okblJqcSMtR4c52UKxDiX9GRBS8+Q=
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
Expand Down Expand Up @@ -94,6 +98,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twpayne/go-jsonstruct/v3 v3.0.0 h1:qdhM8Wiq7BPwjrNRK3n1KtLt4IReiXYWUEZEfjixL0s=
github.com/twpayne/go-jsonstruct/v3 v3.0.0/go.mod h1:p+c9NMNNnoc1z+FvWFUQklf0pzFaIlf03CfrLL7Lo2k=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
Expand Down
37 changes: 37 additions & 0 deletions internal/proxy/generate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package proxy

import (
"bytes"
"context"
"embed"
"encoding/json"
"fmt"
"io/fs"
"path"
Expand All @@ -14,6 +16,7 @@ import (
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/telkomindonesia/oapik/internal/util"
"github.com/twpayne/go-jsonstruct/v3"
"golang.org/x/tools/imports"
)

Expand Down Expand Up @@ -46,6 +49,40 @@ func addTemplateFunc(pe ProxyExtension) {
}
return
}
codegen.TemplateFunctions["writeExtensionType"] = func(ops []codegen.OperationDefinition, name string) (string, error) {
jsGenerator := jsonstruct.NewGenerator(
jsonstruct.WithPackageName("nopackage"),
jsonstruct.WithTypeName(name),
)
for _, op := range ops {
m := map[string]interface{}{}
for k, v := range op.Spec.Extensions {
m[strings.TrimPrefix(k, "x-")] = v
}
jsGenerator.ObserveValue(m)
}
b, err := jsGenerator.Generate()
if err != nil {
return "", fmt.Errorf("fail to generate type definition %w", err)
}
b = bytes.Replace(b, []byte("package nopackage\n"), []byte{}, 1)
return string(b), err
}

codegen.TemplateFunctions["writeExtensionData"] = func(op codegen.OperationDefinition) (string, error) {
m := map[string]interface{}{}
for k, v := range op.Spec.Extensions {
m[strings.TrimPrefix(k, "x-")] = v
}

b, err := json.Marshal(m)
if err != nil {
return "", fmt.Errorf("fail to marshall to json: %w", err)
}
b, err = json.Marshal(string(b))

return string(b), err
}
}

type GenerateOptions struct {
Expand Down
20 changes: 19 additions & 1 deletion internal/proxy/templates/proxy/strict/strict-interface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,22 @@ func (s StrictOperationsMap[T]) ToMap() (m map[string]T) {
"{{.OperationId}}": s.{{.OperationId | ucFirst}},
{{- end}}
}
}
}

{{ writeExtensionType . "StrictExtensionsStruct" }}

type StrictOperationsDataStruct struct {
Extension StrictExtensionsStruct
}
var StrictOperationsData = func() (m StrictOperationsMap[StrictOperationsDataStruct]) {
{{- range .}}
{
b := []byte({{ writeExtensionData . }})
if err := json.Unmarshal(b, &m.{{.OperationId}}.Extension); err != nil {
panic(err)
}
}
{{- end}}

return
}()
48 changes: 48 additions & 0 deletions internal/proxy/testoutput/oapi-proxy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/proxy/testoutput/oapi-proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,7 @@ func TestProxy(t *testing.T) {
}
})

t.Run("OperationData", func(t *testing.T) {
assert.Equal(t, "profile", testoutput.StrictOperationsData.GetProfile.Extension.Proxy.Name)
})
}

0 comments on commit 51471b8

Please sign in to comment.