Skip to content

Commit

Permalink
[NO TESTS NEEDED] Refactor generated code
Browse files Browse the repository at this point in the history
Extracted common functionality to util function.

Signed-off-by: Matej Vasek <[email protected]>

<MH: Fixed cherry-pick conflicts>
  • Loading branch information
matejvasek authored and mheon committed Feb 18, 2021
1 parent 3857056 commit 26c1abf
Show file tree
Hide file tree
Showing 89 changed files with 263 additions and 4,779 deletions.
56 changes: 2 additions & 54 deletions pkg/bindings/containers/types_attach_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package containers

import (
"net/url"
"reflect"
"strings"

"github.com/containers/podman/v2/pkg/bindings/util"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

/*
Expand All @@ -16,60 +12,12 @@ This file is generated automatically by go generate. Do not edit.

// Changed
func (o *AttachOptions) Changed(fieldName string) bool {
r := reflect.ValueOf(o)
value := reflect.Indirect(r).FieldByName(fieldName)
return !value.IsNil()
return util.Changed(o, fieldName)
}

// ToParams
func (o *AttachOptions) ToParams() (url.Values, error) {
params := url.Values{}
if o == nil {
return params, nil
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
s := reflect.ValueOf(o)
if reflect.Ptr == s.Kind() {
s = s.Elem()
}
sType := s.Type()
for i := 0; i < s.NumField(); i++ {
fieldName := sType.Field(i).Name
if !o.Changed(fieldName) {
continue
}
fieldName = strings.ToLower(fieldName)
f := s.Field(i)
if reflect.Ptr == f.Kind() {
f = f.Elem()
}
switch {
case util.IsSimpleType(f):
params.Set(fieldName, util.SimpleTypeToParam(f))
case f.Kind() == reflect.Slice:
for i := 0; i < f.Len(); i++ {
elem := f.Index(i)
if util.IsSimpleType(elem) {
params.Add(fieldName, util.SimpleTypeToParam(elem))
} else {
return nil, errors.New("slices must contain only simple types")
}
}
case f.Kind() == reflect.Map:
lowerCaseKeys := make(map[string][]string)
iter := f.MapRange()
for iter.Next() {
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string)
}
s, err := json.MarshalToString(lowerCaseKeys)
if err != nil {
return nil, err
}

params.Set(fieldName, s)
}
}
return params, nil
return util.ToParams(o)
}

// WithDetachKeys
Expand Down
56 changes: 2 additions & 54 deletions pkg/bindings/containers/types_checkpoint_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package containers

import (
"net/url"
"reflect"
"strings"

"github.com/containers/podman/v2/pkg/bindings/util"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

/*
Expand All @@ -16,60 +12,12 @@ This file is generated automatically by go generate. Do not edit.

// Changed
func (o *CheckpointOptions) Changed(fieldName string) bool {
r := reflect.ValueOf(o)
value := reflect.Indirect(r).FieldByName(fieldName)
return !value.IsNil()
return util.Changed(o, fieldName)
}

// ToParams
func (o *CheckpointOptions) ToParams() (url.Values, error) {
params := url.Values{}
if o == nil {
return params, nil
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
s := reflect.ValueOf(o)
if reflect.Ptr == s.Kind() {
s = s.Elem()
}
sType := s.Type()
for i := 0; i < s.NumField(); i++ {
fieldName := sType.Field(i).Name
if !o.Changed(fieldName) {
continue
}
fieldName = strings.ToLower(fieldName)
f := s.Field(i)
if reflect.Ptr == f.Kind() {
f = f.Elem()
}
switch {
case util.IsSimpleType(f):
params.Set(fieldName, util.SimpleTypeToParam(f))
case f.Kind() == reflect.Slice:
for i := 0; i < f.Len(); i++ {
elem := f.Index(i)
if util.IsSimpleType(elem) {
params.Add(fieldName, util.SimpleTypeToParam(elem))
} else {
return nil, errors.New("slices must contain only simple types")
}
}
case f.Kind() == reflect.Map:
lowerCaseKeys := make(map[string][]string)
iter := f.MapRange()
for iter.Next() {
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string)
}
s, err := json.MarshalToString(lowerCaseKeys)
if err != nil {
return nil, err
}

params.Set(fieldName, s)
}
}
return params, nil
return util.ToParams(o)
}

// WithExport
Expand Down
56 changes: 2 additions & 54 deletions pkg/bindings/containers/types_commit_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package containers

import (
"net/url"
"reflect"
"strings"

"github.com/containers/podman/v2/pkg/bindings/util"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

/*
Expand All @@ -16,60 +12,12 @@ This file is generated automatically by go generate. Do not edit.

// Changed
func (o *CommitOptions) Changed(fieldName string) bool {
r := reflect.ValueOf(o)
value := reflect.Indirect(r).FieldByName(fieldName)
return !value.IsNil()
return util.Changed(o, fieldName)
}

// ToParams
func (o *CommitOptions) ToParams() (url.Values, error) {
params := url.Values{}
if o == nil {
return params, nil
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
s := reflect.ValueOf(o)
if reflect.Ptr == s.Kind() {
s = s.Elem()
}
sType := s.Type()
for i := 0; i < s.NumField(); i++ {
fieldName := sType.Field(i).Name
if !o.Changed(fieldName) {
continue
}
fieldName = strings.ToLower(fieldName)
f := s.Field(i)
if reflect.Ptr == f.Kind() {
f = f.Elem()
}
switch {
case util.IsSimpleType(f):
params.Set(fieldName, util.SimpleTypeToParam(f))
case f.Kind() == reflect.Slice:
for i := 0; i < f.Len(); i++ {
elem := f.Index(i)
if util.IsSimpleType(elem) {
params.Add(fieldName, util.SimpleTypeToParam(elem))
} else {
return nil, errors.New("slices must contain only simple types")
}
}
case f.Kind() == reflect.Map:
lowerCaseKeys := make(map[string][]string)
iter := f.MapRange()
for iter.Next() {
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string)
}
s, err := json.MarshalToString(lowerCaseKeys)
if err != nil {
return nil, err
}

params.Set(fieldName, s)
}
}
return params, nil
return util.ToParams(o)
}

// WithAuthor
Expand Down
56 changes: 2 additions & 54 deletions pkg/bindings/containers/types_create_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package containers

import (
"net/url"
"reflect"
"strings"

"github.com/containers/podman/v2/pkg/bindings/util"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

/*
Expand All @@ -16,58 +12,10 @@ This file is generated automatically by go generate. Do not edit.

// Changed
func (o *CreateOptions) Changed(fieldName string) bool {
r := reflect.ValueOf(o)
value := reflect.Indirect(r).FieldByName(fieldName)
return !value.IsNil()
return util.Changed(o, fieldName)
}

// ToParams
func (o *CreateOptions) ToParams() (url.Values, error) {
params := url.Values{}
if o == nil {
return params, nil
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
s := reflect.ValueOf(o)
if reflect.Ptr == s.Kind() {
s = s.Elem()
}
sType := s.Type()
for i := 0; i < s.NumField(); i++ {
fieldName := sType.Field(i).Name
if !o.Changed(fieldName) {
continue
}
fieldName = strings.ToLower(fieldName)
f := s.Field(i)
if reflect.Ptr == f.Kind() {
f = f.Elem()
}
switch {
case util.IsSimpleType(f):
params.Set(fieldName, util.SimpleTypeToParam(f))
case f.Kind() == reflect.Slice:
for i := 0; i < f.Len(); i++ {
elem := f.Index(i)
if util.IsSimpleType(elem) {
params.Add(fieldName, util.SimpleTypeToParam(elem))
} else {
return nil, errors.New("slices must contain only simple types")
}
}
case f.Kind() == reflect.Map:
lowerCaseKeys := make(map[string][]string)
iter := f.MapRange()
for iter.Next() {
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string)
}
s, err := json.MarshalToString(lowerCaseKeys)
if err != nil {
return nil, err
}

params.Set(fieldName, s)
}
}
return params, nil
return util.ToParams(o)
}
56 changes: 2 additions & 54 deletions pkg/bindings/containers/types_diff_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package containers

import (
"net/url"
"reflect"
"strings"

"github.com/containers/podman/v2/pkg/bindings/util"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)

/*
Expand All @@ -16,58 +12,10 @@ This file is generated automatically by go generate. Do not edit.

// Changed
func (o *DiffOptions) Changed(fieldName string) bool {
r := reflect.ValueOf(o)
value := reflect.Indirect(r).FieldByName(fieldName)
return !value.IsNil()
return util.Changed(o, fieldName)
}

// ToParams
func (o *DiffOptions) ToParams() (url.Values, error) {
params := url.Values{}
if o == nil {
return params, nil
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
s := reflect.ValueOf(o)
if reflect.Ptr == s.Kind() {
s = s.Elem()
}
sType := s.Type()
for i := 0; i < s.NumField(); i++ {
fieldName := sType.Field(i).Name
if !o.Changed(fieldName) {
continue
}
fieldName = strings.ToLower(fieldName)
f := s.Field(i)
if reflect.Ptr == f.Kind() {
f = f.Elem()
}
switch {
case util.IsSimpleType(f):
params.Set(fieldName, util.SimpleTypeToParam(f))
case f.Kind() == reflect.Slice:
for i := 0; i < f.Len(); i++ {
elem := f.Index(i)
if util.IsSimpleType(elem) {
params.Add(fieldName, util.SimpleTypeToParam(elem))
} else {
return nil, errors.New("slices must contain only simple types")
}
}
case f.Kind() == reflect.Map:
lowerCaseKeys := make(map[string][]string)
iter := f.MapRange()
for iter.Next() {
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string)
}
s, err := json.MarshalToString(lowerCaseKeys)
if err != nil {
return nil, err
}

params.Set(fieldName, s)
}
}
return params, nil
return util.ToParams(o)
}
Loading

0 comments on commit 26c1abf

Please sign in to comment.