github.com/jwilson-ts/prototool@v1.3.0/internal/format/format.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package format
    22  
    23  import (
    24  	"github.com/uber/prototool/internal/text"
    25  	"go.uber.org/zap"
    26  )
    27  
    28  // Transformer transforms an input file into an output file.
    29  type Transformer interface {
    30  	// Transform transforms the data.
    31  	//
    32  	// Failures should never happen in the CLI tool as we run the files
    33  	// through protoc first, but this is done because we want to verify
    34  	// code correctness here and protect against the bad case.
    35  	Transform(filename string, data []byte) ([]byte, []*text.Failure, error)
    36  }
    37  
    38  // TransformerOption is an option for a new Transformer.
    39  type TransformerOption func(*transformer)
    40  
    41  // TransformerWithLogger returns a TransformerOption that uses the given logger.
    42  //
    43  // The default is to use zap.NewNop().
    44  func TransformerWithLogger(logger *zap.Logger) TransformerOption {
    45  	return func(transformer *transformer) {
    46  		transformer.logger = logger
    47  	}
    48  }
    49  
    50  // TransformerWithFix returns a TransformerOption that will update the file options
    51  // go_package, java_package to match the package per the guidelines of the style guide.
    52  func TransformerWithFix() TransformerOption {
    53  	return func(transformer *transformer) {
    54  		transformer.fix = true
    55  	}
    56  }
    57  
    58  // NewTransformer returns a new Transformer.
    59  func NewTransformer(options ...TransformerOption) Transformer {
    60  	return newTransformer(options...)
    61  }