<commit_before>// Copyright 2015-2017 trivago GmbH
//
// 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 format

import (
	"github.com/trivago/gollum/core"
)

// StreamName formatter plugin
// StreamName is a formatter that prefixes a message with the StreamName.
// Configuration example
//
//  - format.StreamName:
//      Separator: " "
//      UseHistory: false
//      ApplyTo: "payload" # payload or <metaKey>
//
// UseHistory can be set to true to not use the current but the previous
// stream name. This can be useful to e.g. get the name of the stream messages
// were sent to the fallback from. By default this is set to false.
//
// Separator sets the separator character placed after the stream name.
// This is set to " " by default.
//
// ApplyTo defines the formatter content to use
type StreamName struct {
	core.SimpleFormatter
	separator   []byte
	usePrevious bool
}

func init() {
	core.TypeRegistry.Register(StreamName{})
}

// Configure initializes this formatter with values from a plugin config.
func (format *StreamName) Configure(conf core.PluginConfigReader) error {
	format.SimpleFormatter.Configure(conf)

	format.separator = []byte(conf.GetString("Separator", ":"))
	format.usePrevious = conf.GetBool("UseHistory", false)
	return conf.Errors.OrNil()
}

// ApplyFormatter update message payload
func (format *StreamName) ApplyFormatter(msg *core.Message) error {
	separatorLen := len(format.separator)
	streamName := format.getStreamName(msg)

	var payload []byte
	if separatorLen > 0 {
		content := format.GetAppliedContent(msg)

		dataSize := len(streamName) + separatorLen + len(content)
		payload = core.MessageDataPool.Get(dataSize)

		offset := copy(payload, []byte(streamName))
		offset += copy(payload[offset:], format.separator)
		copy(payload[offset:], content)
	} else {
		payload = []byte(streamName)
	}
	
	format.SetAppliedContent(msg, payload)
	return nil
}

func (format *StreamName) getStreamName(msg *core.Message) string {
	var streamName string

	switch {
	case !format.usePrevious:
		streamName = core.StreamRegistry.GetStreamName(msg.StreamID())
	default:
		streamName = core.StreamRegistry.GetStreamName(msg.PreviousStreamID())
	}

	return streamName
}<commit_msg>Code style<commit_after>// Copyright 2015-2017 trivago GmbH
//
// 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 format

import (
	"github.com/trivago/gollum/core"
)

// StreamName formatter plugin
// StreamName is a formatter that prefixes a message with the StreamName.
// Configuration example
//
//  - format.StreamName:
//      Separator: " "
//      UseHistory: false
//      ApplyTo: "payload" # payload or <metaKey>
//
// UseHistory can be set to true to not use the current but the previous
// stream name. This can be useful to e.g. get the name of the stream messages
// were sent to the fallback from. By default this is set to false.
//
// Separator sets the separator character placed after the stream name.
// This is set to " " by default.
//
// ApplyTo defines the formatter content to use
type StreamName struct {
	core.SimpleFormatter
	separator   []byte
	usePrevious bool
}

func init() {
	core.TypeRegistry.Register(StreamName{})
}

// Configure initializes this formatter with values from a plugin config.
func (format *StreamName) Configure(conf core.PluginConfigReader) error {
	format.SimpleFormatter.Configure(conf)

	format.separator = []byte(conf.GetString("Separator", ":"))
	format.usePrevious = conf.GetBool("UseHistory", false)
	return conf.Errors.OrNil()
}

// ApplyFormatter update message payload
func (format *StreamName) ApplyFormatter(msg *core.Message) error {
	separatorLen := len(format.separator)
	streamName := format.getStreamName(msg)

	var payload []byte
	if separatorLen > 0 {
		content := format.GetAppliedContent(msg)

		dataSize := len(streamName) + separatorLen + len(content)
		payload = core.MessageDataPool.Get(dataSize)

		offset := copy(payload, []byte(streamName))
		offset += copy(payload[offset:], format.separator)
		copy(payload[offset:], content)
	} else {
		payload = []byte(streamName)
	}

	format.SetAppliedContent(msg, payload)
	return nil
}

func (format *StreamName) getStreamName(msg *core.Message) string {
	var streamName string

	switch {
	case !format.usePrevious:
		streamName = core.StreamRegistry.GetStreamName(msg.StreamID())
	default:
		streamName = core.StreamRegistry.GetStreamName(msg.PreviousStreamID())
	}

	return streamName
}
{'fraction_non_alphanumeric': 0.10491562729273661, 'fraction_numerical': 0.004768892149669846, 'mean_word_length': 4.025806451612903, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 7, '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': '5962525', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1534, 'n_words': 549}