Repository: grpc/grpc-go
File: internal/xds/rbac/converter.go (Go)

Code before:
		return nil, fmt.Errorf("missing required field: TypedConfig")
	}
	customConfig, loggerName, err := getCustomConfig(loggerConfig.AuditLogger.TypedConfig)
	if err != nil {
		return nil, err
	}
	if loggerName == "" {
		return nil, fmt.Errorf("field TypedConfig.TypeURL cannot be an empty string")
	}
	factory := audit.GetLoggerBuilder(loggerName)
	if factory == nil {
		if loggerConfig.IsOptional {
			return nil, nil
		}
		return nil, fmt.Errorf("no builder registered for %v", loggerName)
	}
	auditLoggerConfig, err := factory.ParseLoggerConfig(customConfig)
	if err != nil {
		return nil, fmt.Errorf("custom config could not be parsed by registered factory. error: %v", err)
	}
	auditLogger := factory.Build(auditLoggerConfig)
	return auditLogger, nil
}

func getCustomConfig(config *anypb.Any) (json.RawMessage, string, error) {
	unmarshalledConfig, err := config.UnmarshalNew()
	if err != nil {
		return nil, "", err
	}
	switch m := unmarshalledConfig.(type) {
	case *v1xdsudpatypepb.TypedStruct:
		return convertCustomConfig(m.TypeUrl, m.Value)
	case *v3xdsxdstypepb.TypedStruct:
		return convertCustomConfig(m.TypeUrl, m.Value)
	case *v3auditloggersstreampb.StdoutAuditLog:
		return convertStdoutConfig(m)
	}
	return nil, "", fmt.Errorf("custom config not implemented for type [%v]", config.GetTypeUrl())
}

func convertStdoutConfig(config *v3auditloggersstreampb.StdoutAuditLog) (json.RawMessage, string, error) {
	json, err := protojson.Marshal(config)
	return json, stdout.Name, err
}

func convertCustomConfig(typeURL string, s *structpb.Struct) (json.RawMessage, string, error) {
	// The gRPC policy name will be the "type name" part of the value of the
	// type_url field in the TypedStruct. We get this by using the part after
	// the last / character. Can assume a valid type_url from the control plane.
	urls := strings.Split(typeURL, "/")
	if len(urls) == 0 {

Review comment (suggestion):
```suggestion
	c, err := config.UnmarshalNew()
```

Code after:
		return nil, fmt.Errorf("missing required field: TypedConfig")
	}
	customConfig, loggerName, err := getCustomConfig(loggerConfig.AuditLogger.TypedConfig)
	if err != nil {
		return nil, err
	}
	if loggerName == "" {
		return nil, fmt.Errorf("field TypedConfig.TypeURL cannot be an empty string")
	}
	factory := audit.GetLoggerBuilder(loggerName)
	if factory == nil {
		if loggerConfig.IsOptional {
			return nil, nil
		}
		return nil, fmt.Errorf("no builder registered for %v", loggerName)
	}
	auditLoggerConfig, err := factory.ParseLoggerConfig(customConfig)
	if err != nil {
		return nil, fmt.Errorf("custom config could not be parsed by registered factory. error: %v", err)
	}
	auditLogger := factory.Build(auditLoggerConfig)
	return auditLogger, nil
}

func getCustomConfig(config *anypb.Any) (json.RawMessage, string, error) {
	c, err := config.UnmarshalNew()
	if err != nil {
		return nil, "", err
	}
	switch m := c.(type) {
	case *v1xdsudpatypepb.TypedStruct:
		return convertCustomConfig(m.TypeUrl, m.Value)
	case *v3xdsxdstypepb.TypedStruct:
		return convertCustomConfig(m.TypeUrl, m.Value)
	case *v3auditloggersstreampb.StdoutAuditLog:
		return convertStdoutConfig(m)
	}
	return nil, "", fmt.Errorf("custom config not implemented for type [%v]", config.GetTypeUrl())
}

func convertStdoutConfig(config *v3auditloggersstreampb.StdoutAuditLog) (json.RawMessage, string, error) {
	json, err := protojson.Marshal(config)
	return json, stdout.Name, err
}

func convertCustomConfig(typeURL string, s *structpb.Struct) (json.RawMessage, string, error) {
	// The gRPC policy name will be the "type name" part of the value of the
	// type_url field in the TypedStruct. We get this by using the part after
	// the last / character. Can assume a valid type_url from the control plane.
	urls := strings.Split(typeURL, "/")
	if len(urls) == 0 {