Skip to content

Instantly share code, notes, and snippets.

@ericraio
Created July 31, 2020 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericraio/82ce51bf37657e8ddcaaaa5932734ff9 to your computer and use it in GitHub Desktop.
Save ericraio/82ce51bf37657e8ddcaaaa5932734ff9 to your computer and use it in GitHub Desktop.
package lineitem
import (
"errors"
"github.com/ezoic/go-dfpapi/generated/lineitemservice"
)
type LineItemModel struct {
Attributes *lineitemservice.LineItem
}
func (lim *LineItemModel) GetCustomTargetingValuesFromKeyID(targetID int64) ([]*int64, error) {
var values []*int64
targetingPtr := lim.Attributes.Targeting
if targetingPtr == nil {
return values, errors.New("Targeting is missing")
}
targeting := *targetingPtr
customTargetingPtr := targeting.CustomTargeting
if customTargetingPtr == nil {
return values, errors.New("Custom targeting is missing")
}
customTargeting := *customTargetingPtr
children := customTargeting.Children
var childPtr *lineitemservice.CustomCriteriaNode
if len(children) > 1 {
for _, target := range children {
if target == nil {
continue
}
customCriteriaPtr := target.CustomCriteria
if customCriteriaPtr == nil {
continue
}
customCriteria := *customCriteriaPtr
keyIdPtr := customCriteria.KeyId
if keyIdPtr == nil {
continue
}
keyID := *keyIdPtr
if keyID == targetID {
childPtr = target
break
}
}
} else {
childPtr = children[0]
}
if childPtr == nil {
return values, errors.New("Missing child values")
}
child := *childPtr
criteriaPtr := child.CustomCriteria
if criteriaPtr == nil {
return values, errors.New("Missing custom criteria")
}
criteria := *criteriaPtr
return criteria.ValueIds, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment