Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
DanG100 committed Aug 15, 2023
1 parent 6ed850a commit e376432
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dataplane/standalone/apigen/apigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func generate() error {
APIName: nameTrimmed,
}
for _, fn := range iface.Funcs {
meta := saiast.GetFuncMeta(fn, sai)
meta := sai.GetFuncMeta(fn)
tf := createCCData(meta, sai, fn)
ccData.Funcs = append(ccData.Funcs, *tf)
}
Expand Down
10 changes: 5 additions & 5 deletions dataplane/standalone/apigen/docparser/docparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"strings"
)

// Info contains all the info parsed from the doxygen.
type Info struct {
// SAIInfo contains all the info parsed from the doxygen.
type SAIInfo struct {
// attrs is a map from sai type (sai_port_t) to its attributes.
Attrs map[string]*Attr
// attrs is a map from enum name (sai_port_media_type_t) to the values of the enum.
Expand Down Expand Up @@ -97,8 +97,8 @@ type SimpleSect struct {
const xmlPath = "dataplane/standalone/apigen/xml"

// ParseSAIXMLDir parses all the SAI Doxygen XML files in a directory.
func ParseSAIXMLDir() (*Info, error) {
i := &Info{
func ParseSAIXMLDir() (*SAIInfo, error) {
i := &SAIInfo{
Attrs: make(map[string]*Attr),
Enums: make(map[string][]string),
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func memberToEnumValueStrings(enum MemberDef) []string {
}

// parseXMLFile parses a single XML and appends the values into xmlInfo.
func parseXMLFile(file string, xmlInfo *Info) error {
func parseXMLFile(file string, xmlInfo *SAIInfo) error {
b, err := os.ReadFile(file)
if err != nil {
return err
Expand Down
26 changes: 15 additions & 11 deletions dataplane/standalone/apigen/protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

// Generates returns a map of files containing the generated code code.
func Generate(doc *docparser.Info, sai *saiast.SAIAPI) (map[string]string, error) {
func Generate(doc *docparser.SAIInfo, sai *saiast.SAIAPI) (map[string]string, error) {
files := map[string]string{}
common, err := generateCommonTypes(doc)
if err != nil {
Expand All @@ -41,7 +41,7 @@ func Generate(doc *docparser.Info, sai *saiast.SAIAPI) (map[string]string, error
for _, iface := range sai.Ifaces {
apiName := strings.TrimSuffix(strings.TrimPrefix(iface.Name, "sai_"), "_api_t")
for _, fn := range iface.Funcs {
meta := saiast.GetFuncMeta(fn, sai)
meta := sai.GetFuncMeta(fn)
if err := populateTmplDataFromFunc(apis, doc, apiName, meta); err != nil {
return nil, err
}
Expand All @@ -57,7 +57,7 @@ func Generate(doc *docparser.Info, sai *saiast.SAIAPI) (map[string]string, error

// generateCommonTypes returns all contents of the common proto.
// These all reside in the common.proto file to simplify handling imports.
func generateCommonTypes(docInfo *docparser.Info) (string, error) {
func generateCommonTypes(docInfo *docparser.SAIInfo) (string, error) {
common := &protoCommonTmplData{
Enums: map[string]*protoEnum{},
Lists: map[string]*protoTmplMessage{},
Expand Down Expand Up @@ -130,7 +130,7 @@ func generateCommonTypes(docInfo *docparser.Info) (string, error) {
}

// populateTmplDataFromFunc populatsd the protobuf template struct from a SAI function call.
func populateTmplDataFromFunc(apis map[string]*protoAPITmplData, docInfo *docparser.Info, apiName string, meta *saiast.FuncMetadata) error {
func populateTmplDataFromFunc(apis map[string]*protoAPITmplData, docInfo *docparser.SAIInfo, apiName string, meta *saiast.FuncMetadata) error {
if _, ok := apis[apiName]; !ok {
apis[apiName] = &protoAPITmplData{
Enums: make(map[string]protoEnum),
Expand Down Expand Up @@ -256,7 +256,7 @@ func populateTmplDataFromFunc(apis map[string]*protoAPITmplData, docInfo *docpar
return nil
}

func createAttrs(startIdx int, xmlInfo *docparser.Info, attrs []*docparser.AttrTypeName, inOneof bool) ([]protoTmplField, error) {
func createAttrs(startIdx int, xmlInfo *docparser.SAIInfo, attrs []*docparser.AttrTypeName, inOneof bool) ([]protoTmplField, error) {
fields := []protoTmplField{}
for _, attr := range attrs {
// Function pointers are implemented as streaming RPCs instead of settable attributes.
Expand Down Expand Up @@ -896,8 +896,8 @@ message FdbEventNotificationData {
// saiTypeToProtoTypeCompound handles compound sai types (eg list of enums).
// The map key contains the base type (eg list) and func accepts the subtype (eg an enum type)
// and returns the full type string (eg repeated sample_enum).
var saiTypeToProtoTypeCompound = map[string]func(subType string, xmlInfo *docparser.Info, inOneof bool) (string, bool){
"sai_s32_list_t": func(subType string, xmlInfo *docparser.Info, inOneof bool) (string, bool) {
var saiTypeToProtoTypeCompound = map[string]func(subType string, xmlInfo *docparser.SAIInfo, inOneof bool) (string, bool){
"sai_s32_list_t": func(subType string, xmlInfo *docparser.SAIInfo, inOneof bool) (string, bool) {
if _, ok := xmlInfo.Enums[subType]; !ok {
return "", false
}
Expand All @@ -906,14 +906,18 @@ var saiTypeToProtoTypeCompound = map[string]func(subType string, xmlInfo *docpar
}
return "repeated " + saiast.TrimSAIName(subType, true, false), true
},
"sai_acl_field_data_t": func(next string, xmlInfo *docparser.Info, inOneof bool) (string, bool) { return "AclFieldData", false },
"sai_acl_action_data_t": func(next string, xmlInfo *docparser.Info, inOneof bool) (string, bool) { return "AclActionData", false },
"sai_pointer_t": func(next string, xmlInfo *docparser.Info, inOneof bool) (string, bool) { return "-", false }, // Noop, these are special cases.
"sai_acl_field_data_t": func(next string, xmlInfo *docparser.SAIInfo, inOneof bool) (string, bool) {
return "AclFieldData", false
},
"sai_acl_action_data_t": func(next string, xmlInfo *docparser.SAIInfo, inOneof bool) (string, bool) {
return "AclActionData", false
},
"sai_pointer_t": func(next string, xmlInfo *docparser.SAIInfo, inOneof bool) (string, bool) { return "-", false }, // Noop, these are special cases.
}

// saiTypeToProtoType returns the protobuf type string for a SAI type.
// example: sai_u8_list_t -> repeated uint32
func saiTypeToProtoType(saiType string, xmlInfo *docparser.Info, inOneof bool) (string, bool, error) {
func saiTypeToProtoType(saiType string, xmlInfo *docparser.SAIInfo, inOneof bool) (string, bool, error) {
saiType = strings.TrimPrefix(saiType, "const ")

if pt, ok := saiTypeToProto[saiType]; ok {
Expand Down
2 changes: 1 addition & 1 deletion dataplane/standalone/apigen/saiast/saiast.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type FuncMetadata struct {
}

// GetFuncMeta returns the metadata for a SAI func.
func GetFuncMeta(fn *TypeDecl, sai *SAIAPI) *FuncMetadata {
func (sai *SAIAPI) GetFuncMeta(fn *TypeDecl) *FuncMetadata {
meta := &FuncMetadata{}
meta.Name = strings.TrimSuffix(strings.TrimPrefix(fn.Name, "sai_"), "_fn")
matches := funcExpr.FindStringSubmatch(meta.Name)
Expand Down

0 comments on commit e376432

Please sign in to comment.