-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructure_filter.go
82 lines (71 loc) · 2.29 KB
/
structure_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gmailfilter
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/gmail/v1"
)
func setFilterValuesToState(d *schema.ResourceData, filter *gmail.Filter) error {
if err := d.Set("action", []interface{}{flattenFilterAction(filter.Action)}); err != nil {
return fmt.Errorf("error setting action: %s", err)
}
if err := d.Set("criteria", []interface{}{flattenFilterCriteria(filter.Criteria)}); err != nil {
return fmt.Errorf("error setting criteria: %s", err)
}
return nil
}
func expandFilterAction(d *schema.ResourceData) *gmail.FilterAction {
raw, ok := d.Get("action").([]interface{})
if !ok || len(raw) == 0 {
return nil
}
in, ok := raw[0].(map[string]interface{})
if !ok {
return nil
}
return &gmail.FilterAction{
AddLabelIds: expandStringSlice(in["add_label_ids"]),
Forward: in["forward"].(string),
RemoveLabelIds: expandStringSlice(in["remove_label_ids"]),
}
}
func flattenFilterAction(action *gmail.FilterAction) map[string]interface{} {
return map[string]interface{}{
"add_label_ids": action.AddLabelIds,
"forward": action.Forward,
"remove_label_ids": action.RemoveLabelIds,
}
}
func flattenFilterCriteria(criteria *gmail.FilterCriteria) map[string]interface{} {
return map[string]interface{}{
"exclude_chats": criteria.ExcludeChats,
"from": criteria.From,
"has_attachment": criteria.HasAttachment,
"negated_query": criteria.NegatedQuery,
"query": criteria.Query,
"size": criteria.Size,
"size_comparison": criteria.SizeComparison,
"subject": criteria.Subject,
"to": criteria.To,
}
}
func expandFilterCriteria(d *schema.ResourceData) *gmail.FilterCriteria {
raw, ok := d.Get("criteria").([]interface{})
if !ok || len(raw) == 0 {
return nil
}
in, ok := raw[0].(map[string]interface{})
if !ok {
return nil
}
return &gmail.FilterCriteria{
ExcludeChats: in["exclude_chats"].(bool),
From: in["from"].(string),
HasAttachment: in["has_attachment"].(bool),
NegatedQuery: in["negated_query"].(string),
Query: in["query"].(string),
Size: int64(in["size"].(int)),
SizeComparison: in["size_comparison"].(string),
Subject: in["subject"].(string),
To: in["to"].(string),
}
}