-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
2,950 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package astbuilder | ||
|
||
import ( | ||
"github.com/dave/dst" | ||
) | ||
|
||
// CompositeLiteralDetails captures the information required to generate code for an inline struct initialization | ||
type CompositeLiteralDetails struct { | ||
structType dst.Expr | ||
elts []dst.Expr | ||
} | ||
|
||
// NewCompositeLiteralDetails creates a new instance for initialization of the specified struct | ||
// structType is an expression to handle both structs from the current package and imported ones requiring qualification | ||
func NewCompositeLiteralDetails(structType dst.Expr) *CompositeLiteralDetails { | ||
return &CompositeLiteralDetails{ | ||
structType: structType, | ||
} | ||
} | ||
|
||
// AddField adds initialization of another field | ||
// Returns the receiver to allow method chaining when desired | ||
func (details *CompositeLiteralDetails) AddField(name string, value dst.Expr) { | ||
expr := &dst.KeyValueExpr{ | ||
Key: dst.NewIdent(name), | ||
Value: dst.Clone(value).(dst.Expr), | ||
} | ||
|
||
expr.Decs.Before = dst.NewLine | ||
expr.Decs.After = dst.NewLine | ||
|
||
details.elts = append(details.elts, expr) | ||
} | ||
|
||
// Build constructs the actual dst.CompositeLit that's required | ||
func (details CompositeLiteralDetails) Build() *dst.CompositeLit { | ||
return &dst.CompositeLit{ | ||
Type: details.structType, | ||
Elts: details.elts, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.