github.com/blend/go-sdk@v1.20220411.3/sourceutil/substitute_string.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package sourceutil
     9  
    10  import (
    11  	"context"
    12  	"strings"
    13  )
    14  
    15  // StringSubstitution is a mutator for a string.
    16  // It returns the modified string, and a bool if the rule matched or not.
    17  type StringSubstitution func(context.Context, string) (string, bool)
    18  
    19  // SubstituteString rewrites a string literal.
    20  func SubstituteString(before, after string) StringSubstitution {
    21  	return func(ctx context.Context, contents string) (string, bool) {
    22  		if !strings.Contains(contents, before) {
    23  			return "", false
    24  		}
    25  		return strings.Replace(contents, before, after, -1), true
    26  	}
    27  }