Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippHeuer committed Jul 1, 2024
0 parents commit d21b330
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# goland
.idea/*

# generated artifacts
.dist/*
.tmp/*
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PrimeCodeGen - Go Libraries

> Libraries for reusable code used in PrimeLib code generation templates.
11 changes: 11 additions & 0 deletions requeststruct/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/primelib/primecodegen-lib-go/requeststruct

go 1.21

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions requeststruct/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
104 changes: 104 additions & 0 deletions requeststruct/requeststruct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package requeststruct

import (
"net/url"
"reflect"
)

type RequestParams struct {
HeaderParams map[string]string
CookieParams map[string]string
PathParams map[string]string
QueryParams url.Values
BodyParam interface{}
}

func ResolveRequestParams(requestStruct any) (RequestParams, error) {
result := RequestParams{
HeaderParams: map[string]string{},
CookieParams: map[string]string{},
PathParams: map[string]string{},
QueryParams: url.Values{},
BodyParam: nil,
}
structType := reflect.TypeOf(requestStruct)
structVal := reflect.ValueOf(requestStruct)

for i := 0; i < structType.NumField(); i++ {
fieldType := structType.Field(i)
valType := structVal.Field(i)
tagValue := fieldType.Tag

// headerParam
if val, ok := tagValue.Lookup("headerParam"); ok {
// parse
data := parseKVTags(val)
name := data["name"]
style := data["style"]
if style != "simple" {
style = "simple"
}
explode := data["explode"]
if explode != "true" {
explode = "false"
}

// add to result
result.HeaderParams[name] = ResolveParameterValue(valType, nil)
}

// cookieParams
if val, ok := tagValue.Lookup("cookieParam"); ok {
// parse
data := parseKVTags(val)
name := data["name"]
style := data["style"]
if style != "form" && style != "spaceDelimited" && style != "pipeDelimited" && style != "deepObject" {
style = "form"
}

// add to result
result.CookieParams[name] = ResolveParameterValue(valType, nil)
}

// pathParam
if val, ok := tagValue.Lookup("pathParam"); ok {
// parse
data := parseKVTags(val)
name := data["name"]
style := data["style"]
if style != "simple" && style != "label" && style != "matrix" {
style = "simple"
}
explode := data["explode"]
if explode != "true" {
explode = "false"
}

// add to result
result.PathParams[name] = ResolveParameterValue(valType, nil)
}

// queryParam
if val, ok := tagValue.Lookup("queryParam"); ok {
// parse
data := parseKVTags(val)
name := data["name"]
style := data["style"]
if style != "form" && style != "spaceDelimited" && style != "pipeDelimited" && style != "deepObject" {
style = "form"
}

// add to result
result.QueryParams.Add(name, ResolveParameterValue(valType, nil))
}

// bodyParam
if _, ok := tagValue.Lookup("bodyParam"); ok {
// add to result
result.BodyParam = valType.Interface()
}
}

return result, nil
}
47 changes: 47 additions & 0 deletions requeststruct/requeststruct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package requeststruct

import (
"net/url"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

// Sample struct to test ResolveRequestParams
type TestRequest struct {
StringField *string `headerParam:"name=string_field,style=simple,explode=false"`
IntField *int `cookieParam:"name=int_field,style=form"`
BoolField *bool `pathParam:"name=bool_field,style=label"`
TimeField *time.Time `queryParam:"name=time_field,style=form"`
MissingField *float64 // No tag
SomeStruct interface{} `bodyParam:""`
}

func TestResolveRequestParams(t *testing.T) {
stringField := "string_value"
intField := 42
boolField := true
timeField := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)

req := TestRequest{
StringField: &stringField,
IntField: &intField,
BoolField: &boolField,
TimeField: &timeField,
MissingField: nil,
SomeStruct: 5,
}

expected := RequestParams{
HeaderParams: map[string]string{"string_field": "string_value"},
CookieParams: map[string]string{"int_field": "42"},
PathParams: map[string]string{"bool_field": "true"},
QueryParams: url.Values{"time_field": {timeField.Format(time.RFC3339)}},
BodyParam: 5,
}

result, err := ResolveRequestParams(req)
assert.NoError(t, err)
assert.Equal(t, expected, result)
}
20 changes: 20 additions & 0 deletions requeststruct/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package requeststruct

import (
"strings"
)

func parseKVTags(tag string) map[string]string {
kv := map[string]string{}

for _, part := range strings.Split(tag, ",") {
parts := strings.Split(part, "=")
if len(parts) != 2 {
continue
}

kv[parts[0]] = parts[1]
}

return kv
}
52 changes: 52 additions & 0 deletions requeststruct/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package requeststruct

import (
"reflect"
"strconv"
"time"
)

type ValueToStringConfig struct {
TimeFormat string // TimeFormat is the format to use for time.Time values.
}

var defaultValToStringConfig = &ValueToStringConfig{
TimeFormat: time.RFC3339,
}

// ResolveParameterValue resolves the value of a field in a struct into a string to pass as request parameter.
func ResolveParameterValue(value reflect.Value, cfg *ValueToStringConfig) string {
if cfg == nil {
cfg = defaultValToStringConfig
}

// ptr
if value.Kind() == reflect.Pointer {
if value.IsNil() {
return ""
}
value = value.Elem()
}

// value
switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(value.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(value.Uint(), 10)
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(value.Float(), 'f', -1, 64)
case reflect.String:
return value.String()
case reflect.Bool:
return strconv.FormatBool(value.Bool())
case reflect.Struct:
if value.Type().PkgPath() == "time" && value.Type().Name() == "Time" {
return value.Interface().(time.Time).Format(cfg.TimeFormat)
}
default:
return ""
}

return ""
}
45 changes: 45 additions & 0 deletions requeststruct/value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package requeststruct

import (
"reflect"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

type nestedStruct struct {
Field1 string
Field2 int
}

func TestResolveParameterValue(t *testing.T) {
query := "example"
limit := 10
active := true
date := time.Now()
uintField := uint(100)
floatField := 3.14
nested := nestedStruct{Field1: "nested", Field2: 2}

tests := []struct {
value interface{}
expected string
}{
{&query, "example"},
{&limit, "10"},
{&active, "true"},
{&date, date.Format(time.RFC3339)},
{&uintField, strconv.FormatUint(uint64(uintField), 10)},
{&floatField, strconv.FormatFloat(floatField, 'f', -1, 64)},
{nil, ""},
{&nested, ""}, // nested structs are not supported yet
}

for _, tt := range tests {
val := reflect.ValueOf(tt.value)
result := ResolveParameterValue(val, nil)
assert.Equal(t, tt.expected, result)
}
}
17 changes: 17 additions & 0 deletions resty/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/primelib/primecodegen-lib-go/resty

go 1.22

require go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0

require (
github.com/dubonzi/otelresty v1.3.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-resty/resty/v2 v2.12.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/net v0.22.0 // indirect
)
74 changes: 74 additions & 0 deletions resty/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dubonzi/otelresty v1.3.0 h1:CxQEPCn26DaDJLV/1kmbxud1m5Gv4ZY0n9rXHD6cMmw=
github.com/dubonzi/otelresty v1.3.0/go.mod h1:vUZlU7AozHcWC2KWDAJssyQLboSBrOgUhYQgM5mJ1PE=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-resty/resty/v2 v2.12.0 h1:rsVL8P90LFvkUYq/V5BTVe203WfRIU4gvcf+yfzJzGA=
github.com/go-resty/resty/v2 v2.12.0/go.mod h1:o0yGPrkS3lOe1+eFajk6kBW8ScXzwU3hD69/gt2yB/0=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit d21b330

Please sign in to comment.