<commit_before>// Copyright 2021 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package structmask implements a functionality similar to
// google.protobuf.FieldMask, but which applies only to google.protobuf.Struct.
//
// A google.protobuf.FieldMask can refer only to valid protobuf fields and
// "google.golang.org/protobuf" asserts that when serializing the field mask.
// It makes this mechanism unusable for targeting "unusual" struct fields
// (for example ones containing '.'). Additionally, google.protobuf.FieldMask
// doesn't support wildcard matches (with '*', since it is not a valid proto
// field name).
package structmask

import (
	"fmt"

	"google.golang.org/protobuf/types/known/structpb"
)

// Filter knows how to use StructMask to filter google.protobuf.Struct.
//
// Construct it using NewFilter.
type Filter struct {
	root *node
}

// NewFilter returns a filter that filters structs according to the struct mask.
//
// Returns an error if the struct mask is malformed. If `mask` is empty, returns
// a filter that doesn't actually filter anything.
func NewFilter(mask []*StructMask) (*Filter, error) {
	root, err := parseMask(mask)
	if err != nil {
		return nil, err
	}
	return &Filter{root}, nil
}

// Apply returns a shallow copy of the struct, selecting only elements matching
// the mask.
//
// The result may reuse fields of the original struct (i.e. it copies pointers,
// whenever possible, not actual objects). In extreme case of mask `*` it will
// return `s` as is.
//
// If you need to modify the result, consider explicitly making a deep copy with
// proto.Clone first.
func (f *Filter) Apply(s *structpb.Struct) *structpb.Struct {
	if f.root == nil || f.root == leafNode {
		return s
	}
	filtered := f.root.filterStruct(s)
	if filtered == nil {
		return &structpb.Struct{}
	}
	// During merging we use `nil` as a stand in for "empty set after filtering",
	// in lists to make it distinct from NullValue representing the real `null`.
	// `nil` is not allowed in *structpb.Struct. Convert them all to real Nulls.
	fillNulls(filtered)
	return filtered.GetStructValue()<commit_msg>[structmask] Applying a filter to `nil` is `nil`.<commit_after>// Copyright 2021 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package structmask implements a functionality similar to
// google.protobuf.FieldMask, but which applies only to google.protobuf.Struct.
//
// A google.protobuf.FieldMask can refer only to valid protobuf fields and
// "google.golang.org/protobuf" asserts that when serializing the field mask.
// It makes this mechanism unusable for targeting "unusual" struct fields
// (for example ones containing '.'). Additionally, google.protobuf.FieldMask
// doesn't support wildcard matches (with '*', since it is not a valid proto
// field name).
package structmask

import (
	"fmt"

	"google.golang.org/protobuf/types/known/structpb"
)

// Filter knows how to use StructMask to filter google.protobuf.Struct.
//
// Construct it using NewFilter.
type Filter struct {
	root *node
}

// NewFilter returns a filter that filters structs according to the struct mask.
//
// Returns an error if the struct mask is malformed. If `mask` is empty, returns
// a filter that doesn't actually filter anything.
func NewFilter(mask []*StructMask) (*Filter, error) {
	root, err := parseMask(mask)
	if err != nil {
		return nil, err
	}
	return &Filter{root}, nil
}

// Apply returns a shallow copy of the struct, selecting only elements matching
// the mask.
//
// The result may reuse fields of the original struct (i.e. it copies pointers,
// whenever possible, not actual objects). In extreme case of mask `*` it will
// return `s` as is.
//
// If you need to modify the result, consider explicitly making a deep copy with
// proto.Clone first.
//
// If given `nil`, returns `nil` as well.
func (f *Filter) Apply(s *structpb.Struct) *structpb.Struct {
	if s == nil || f.root == nil || f.root == leafNode {
		return s
	}
	filtered := f.root.filterStruct(s)
	if filtered == nil {
		return &structpb.Struct{}
	}
	// During merging we use `nil` as a stand in for "empty set after filtering",
	// in lists to make it distinct from NullValue representing the real `null`.
	// `nil` is not allowed in *structpb.Struct. Convert them all to real Nulls.
	fillNulls(filtered)
	return filtered.GetStructValue()
{'fraction_non_alphanumeric': 0.10235927921233513, 'fraction_numerical': 0.002972320267508824, 'mean_word_length': 3.551141166525782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1672080', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1455, 'n_words': 693}