Created
August 31, 2017 21:08
-
-
Save slofurno/ced64acd9434b6288dd545a6ec3a77c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tree | |
import ( | |
"encoding/json" | |
"fmt" | |
"testing" | |
"github.com/Getsidecar/partner-api-gateway/models" | |
) | |
func makeCrit(id, parent int64, dimType, value string) models.AdGroupCriterion { | |
return models.AdGroupCriterion{ | |
Id: id, | |
Criterion: models.ProductPartition{ | |
ParentCriterionId: parent, | |
Condition: models.ProductCondition{ | |
Attribute: value, | |
Operand: dimType, | |
}, | |
}, | |
} | |
} | |
func TestBuildTree(t *testing.T) { | |
crits := []models.AdGroupCriterion{ | |
makeCrit(1, 0, "", ""), | |
makeCrit(2, 1, "brand", "brand1"), | |
makeCrit(3, 1, "brand", "brand2"), | |
makeCrit(4, 1, "brand", "brand3"), | |
makeCrit(11, 1, "brand", ""), | |
makeCrit(5, 2, "condition", "new"), | |
makeCrit(6, 2, "condition", "used"), | |
makeCrit(12, 2, "condition", ""), | |
makeCrit(7, 3, "condition", "new"), | |
makeCrit(8, 3, "condition", "used"), | |
makeCrit(13, 3, "condition", ""), | |
makeCrit(9, 4, "condition", "new"), | |
makeCrit(10, 4, "condition", "used"), | |
makeCrit(14, 4, "condition", ""), | |
makeCrit(15, 5, "type", "type1"), | |
makeCrit(16, 5, "type", "type2"), | |
makeCrit(17, 5, "type", "type3"), | |
makeCrit(18, 5, "type", ""), | |
} | |
root := BuildTree(crits) | |
b, _ := json.Marshal(root) | |
fmt.Println(string(b)) | |
printTree(root, 0) | |
} | |
func printTree(root *Tree, depth int) { | |
if root == nil { | |
return | |
} | |
for i := 0; i < depth; i++ { | |
fmt.Printf(" ") | |
} | |
fmt.Printf( | |
"id: %d, type: %s, value: %s\n", | |
root.Partition.Id, | |
root.Partition.Criterion.Condition.Operand, | |
root.Partition.Criterion.Condition.Attribute, | |
) | |
for _, n := range root.Partitions { | |
printTree(n, depth+1) | |
} | |
printTree(root.OPP, depth+1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment