Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Schema functions for mapping rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Gromov committed Jul 11, 2017
1 parent f6d7952 commit f3e059e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
43 changes: 43 additions & 0 deletions rules/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type mappingRuleSnapshot struct {
tombstoned bool
cutoverNanos int64
filter filters.Filter
rawFilters map[string]string
policies []policy.Policy
}

Expand All @@ -64,6 +65,7 @@ func newMappingRuleSnapshot(
cutoverNanos: r.CutoverTime,
filter: filter,
policies: policies,
rawFilters: r.TagFilters,
}, nil
}

Expand Down Expand Up @@ -123,3 +125,44 @@ func (mc *mappingRule) activeIndex(timeNanos int64) int {
}
return idx
}

// Schema returns the given MappingRuleSnapshot in protobuf form.
func (mrs mappingRuleSnapshot) Schema() (*schema.MappingRuleSnapshot, error) {
res := &schema.MappingRuleSnapshot{
Name: mrs.name,
Tombstoned: mrs.tombstoned,
CutoverTime: mrs.cutoverNanos,
TagFilters: mrs.rawFilters,
}

policies := make([]*schema.Policy, len(mrs.policies))
for i, p := range mrs.policies {
policy, err := p.Schema()
if err != nil {
return nil, err
}
policies[i] = policy
}
res.Policies = policies

return res, nil
}

// Schema returns the given MappingRule in protobuf form.
func (mc mappingRule) Schema() (*schema.MappingRule, error) {
res := &schema.MappingRule{
Uuid: mc.uuid,
}

snapshots := make([]*schema.MappingRuleSnapshot, len(mc.snapshots))
for i, s := range mc.snapshots {
snapshot, err := s.Schema()
if err != nil {
return nil, err
}
snapshots[i] = snapshot
}
res.Snapshots = snapshots

return res, nil
}
22 changes: 22 additions & 0 deletions rules/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,25 @@ func TestMappingRuleActiveRuleFound_First(t *testing.T) {
require.NoError(t, err)
require.Equal(t, mr, mr.ActiveRule(20000))
}

func TestMappingRuleSnapshotSchema(t *testing.T) {
expectedSchema := testMappingRuleSchema.Snapshots[0]
mr, err := newMappingRule(testMappingRuleSchema, testTagsFilterOptions())
require.NoError(t, err)
schema, err := mr.snapshots[0].Schema()
require.NoError(t, err)
require.EqualValues(t, expectedSchema, schema)
}

func TestMappingRuleSchema(t *testing.T) {
expected := &schema.MappingRule{
Uuid: testMappingRuleSchema.Uuid,
Snapshots: testMappingRuleSchema.Snapshots[:1],
}

mr, err := newMappingRule(expected, testTagsFilterOptions())
require.NoError(t, err)
schema, err := mr.Schema()
require.NoError(t, err)
require.Equal(t, expected, schema)
}

0 comments on commit f3e059e

Please sign in to comment.