Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Aug 23, 2024
2 parents 0a379dd + 9f51e97 commit cc0659e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
9 changes: 9 additions & 0 deletions service/session/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (
kindLocator *locator.KindLocator
namedParameters state.NamedParameters
locatorOptions []locator.Option //resousrce, route level options
locatorOpt *locator.Options
codecOptions []codec.Option
types []*state.Type
indirectState bool
Expand All @@ -26,6 +27,12 @@ type (
Option func(o *Options)
)

func (o *Options) HasInputParameters() bool {
if o.locatorOpt == nil {
return false
}
return len(o.locatorOpt.InputParameters) > 0
}
func (o *Options) shallReportNotAssignable() bool {
if o.reportNotAssignable == nil {
return true
Expand All @@ -39,6 +46,7 @@ func (o *Options) Indirect(flag bool, options ...locator.Option) *Options {
ret.locatorOptions = append(ret.locatorOptions, options...)
ret.kindLocator = locator.NewKindsLocator(ret.kindLocator, ret.locatorOptions...)
}
ret.locatorOpt = locator.NewOptions(ret.locatorOptions)
return &ret
}

Expand Down Expand Up @@ -89,6 +97,7 @@ func WithLocators(locators *locator.KindLocator) Option {
func WithLocatorOptions(options ...locator.Option) Option {
return func(s *Options) {
s.locatorOptions = options
s.locatorOpt = locator.NewOptions(options)
}
}

Expand Down
5 changes: 4 additions & 1 deletion service/session/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ func (s *Session) viewLookupOptions(aView *view.View, parameters state.NamedPara
result = append(result, locator.WithParameterLookup(func(ctx context.Context, parameter *state.Parameter) (interface{}, bool, error) {
return s.LookupValue(ctx, parameter, opts)
}))
result = append(result, locator.WithInputParameters(parameters))

if !opts.HasInputParameters() {
result = append(result, locator.WithInputParameters(parameters))
}
result = append(result, locator.WithReadInto(s.ReadInto))
viewState := s.state.Lookup(aView)
result = append(result, locator.WithState(viewState.Template))
Expand Down
23 changes: 10 additions & 13 deletions view/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,21 @@ func (r *Resource) mergeParameters(resource *Resource) {
if len(resource.Parameters) == 0 {
return
}
views := r.paramByName()
namedParameters := r.Parameters.Index()
byKindNameParameters := r.Parameters.ByKindName()
for i, candidate := range resource.Parameters {
_, ok := views[candidate.Name]
if _, ok := byKindNameParameters[candidate.In.Name]; ok {
byKindNameParameters[candidate.In.Name].Value = candidate.Value
continue
}
_, ok := namedParameters[candidate.Name]
if !ok {
param := *resource.Parameters[i]
r.Parameters = append(r.Parameters, &param)
} else {
namedParameters[candidate.Name].Value = candidate.Value
}

}
}

Expand Down Expand Up @@ -272,17 +280,6 @@ func (r *Resource) MBusResourceByName() MessageBuses {
return MessageBusSlice(r.MessageBuses).Index()
}

func (r *Resource) paramByName() map[string]*state.Parameter {
index := map[string]*state.Parameter{}
if len(r.Parameters) == 0 {
return index
}
for i, param := range r.Parameters {
index[param.Name] = r.Parameters[i]
}
return index
}

func (r *Resource) typeByName() map[string]*TypeDefinition {
index := map[string]*TypeDefinition{}
if len(r.Parameters) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion view/state/kind/locator/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func WithInputParameters(parameters state.NamedParameters) Option {
for k, v := range parameters {
o.InputParameters[k] = v
if v.In.Kind == state.KindConst {
o.resourceConstants[v.Name] = v.Value
o.resourceConstants[v.In.Name] = v.Value
}
}

Expand Down
18 changes: 18 additions & 0 deletions view/state/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,24 @@ func (p Parameters) Lookup(name string) *Parameter {
return nil
}

// ByKindName indexes parameters by Parameter.In.Name
func (p Parameters) ByKindName() NamedParameters {
result := NamedParameters(make(map[string]*Parameter))
for i, parameter := range p {
if _, ok := result[parameter.In.Name]; ok {
continue
}
result[parameter.In.Name] = p[i]
for _, item := range parameter.Object {
result[item.In.Name] = item
}
for _, item := range parameter.Repeated {
result[item.In.Name] = item
}
}
return result
}

// Index indexes parameters by Parameter.Name
func (p Parameters) Index() NamedParameters {
result := NamedParameters(make(map[string]*Parameter))
Expand Down

0 comments on commit cc0659e

Please sign in to comment.